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\$

1 Answer 1

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.

\$\endgroup\$
1
  • \$\begingroup\$ Don't forget to mark your own answer as valid. \$\endgroup\$
    – Leo
    Commented Mar 6, 2018 at 12:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .