2
\$\begingroup\$

I am making an elevator that passes through the terrain. The character should be allowed to pass through the ground temporarily when he is within the sphere collider. The code is as follows:

using UnityEngine;
using System.Collections;

public class TerrainHole : MonoBehaviour {

    public Collider player; // assign in inspector?
    public Terrain terrain; // assign in inspector?

    void OnTriggerEnter (Collider c) {      
        if (c.tag == "Player") {
            Physics.IgnoreCollision(player.GetComponent<Collider>(), terrain.GetComponent<Collider>(), true);
        }
    }

    void OnTriggerExit (Collider c) {
        if (c.tag == "Player") {
            Physics.IgnoreCollision(player.GetComponent<Collider>(), terrain.GetComponent<Collider>(), false);
        }
    }

}

By all accounts this should work, but I can't get any variation of it to work so far.

What am I doing wrong?

\$\endgroup\$
2
\$\begingroup\$

Small oversight on my part:

using UnityEngine;
using System.Collections;

public class TerrainHole : MonoBehaviour {

    public Collider terrainCollider; // assign in inspector

    void OnTriggerEnter (Collider c) {      
        if (c.CompareTag("Player")) {
            Physics.IgnoreCollision(c, terrainCollider, true);
        }
    }

    void OnTriggerExit (Collider c) {
        if (c.CompareTag("Player")) {
            Physics.IgnoreCollision(c, terrainCollider, false);
        }
    }    
}

I was referencing Collider player instead of Collider c. Since the player's collider is passed as an argument, there's no need to hold a reference to the player as a member variable.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Don't forget to mark your own answer as valid. \$\endgroup\$ – Leo Mar 6 '18 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.