So I have the following script on a few empties with box colliders set over water so that if you are flying, you cannot fly over the water, but you can walk into the water. It works great for the character, however other objects will still collide with it. I've put Debug.Log on the script and I'm getting them to register and no errors are showing. Please let me know if you see a reason for why other object would still collide with the object this script is on or another way to do this if not. Thanks!
public GameObject text;
public GameObject player;
Collider detector;
void Start () {
detector=GetComponent();
}
void OnCollisionEnter (Collision col)
{
if (col.gameObject==player)
{
if (player.GetComponent().hovering)
{
text.SetActive (true);
}else {
Physics.IgnoreCollision (player.GetComponent(),GetComponent());
}
}else{
if (col.gameObject.GetComponent() !=null)
{
Physics.IgnoreCollision (col.gameObject.GetComponent(),GetComponent());
}else if (col.gameObject.GetComponentInChildren() !=null)
{
Physics.IgnoreCollision (col.gameObject.GetComponentInChildren(),GetComponent());
}
}
}
void OnCollisionExit (Collision col)
{
if (col.gameObject==player)
{
text.SetActive (false);
}
}
↧