Is there a way to use a collision detection as a condition for an if statement? I want to make an object travel to an area which i have marked with a sphere collider that will become a random size on scene load. Using OnTriggerEnter won’t work because I need another condition to be fulfilled as well for this to work.I prefer C#. Any ideas?
Here is a script I place on the object that is going to collide with something. I tag items that I want to check for collisions, so some collision volumes can be ignored if needed.
public class CollideCheck : MonoBehaviour
{
private bool isCollided;
public bool IsCollided
{
get {return isCollided; }
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Obstacle")
{
isCollided = true;
}
}
public void ResetCollision()
{
isCollided = false;
}
}
Then in the code that is monitoring for collisions, grab a reference to the CollideCheck script, do the check, and reset the collision when one is detected:
collideCheck = myGameObject.GetComponent<CollideCheck>();
if (collideCheck .IsCollided() && whatever other conditions you want)
{
collideCheck .ResetCollision();
}