Check Object Is Not Colliding

Hii…

I have one gameObject with jump animation.

I want to check while end of jump animation whether gameObject is colliding or not.
If gameObject collides with collider then object will move forward otherwise with gravity force , it will fall down.

But i stuck at one point , How do i find object is not colliding with collider?

Code :

void Update () {

		if (moveForward) {
			transform.Translate (Vector3.right * Time.deltaTime * speed, Space.World);
		} else {
			transform.Translate (Vector3.right * Time.deltaTime * speed, Space.World);
			this.GetComponent <Rigidbody>().useGravity = true;	
		}
			
		if(this.transform.position.y < -5.00f){
			Destroy(this.gameObject);
		}

	}

	void OnCollisionEnter(Collision collision) {

			if (collision.gameObject.tag.Equals ("platform")) {
				
				moveForward = true;
			} 
		}

Here . OnCollisionEnter will call when object collides with platform but what if object does not collide with any of the object. At that time OnCollisionEnter will not be called so that i can set moveForward = false.

Even i have set event at end of the jump animation. Now i can get the point when player will come down while jumping.

Still i am stuck at this point. anyone please help me out. I need ur big help.

Thanks.

A quick and easy solution (possibly needing some fine-tuning, depending on how it’s put together) would be to use a variable to determine whether you’re presumably on the ground or not.

bool isGrounded = true;

// ...

void OnCollisionStay(Collision other)
{
    if(other.CompareTag("platform"))
    {
        isGrounded = true;
    }
}

void OnCollisionExit(Collision other)
{
    if(other.CompareTag("platform"))
    {
        isGrounded = false;
    }
}

Then, your accommodations for moveForward can be based on the “isGrounded” variable. This means you’ll need to ensure that “isGrounded” is always accurate, but if you’re looking to test for an event based on whether the event is occurring or not, this is a way to handle it.

As a side note, I used “CompareTag” in the if statements to improve performance.

If you want to fire event exactly when no collisions happend you need to create a monobehavior class that will keep a bool property (for instance “CollissionDetected”) whether any collision on an game object happend or not and set it to “false” by default. Then create another monobehavior class and attach it to the game object which you whant to check collisions (or no collision) for. In that class use OnCollisionX or OnTriggerX events. Each event has to change you bool property according to it’s goal (for instance OnCollisionEnter, OnCollisionStay or OnTriggerEnter, OnTriggerStay should set bool property to “true” and in OnCollisionExit or OnTriggerExit events set it to “false”). Then in the first class with “CollissionDetected” property in LateUpdate() monobehavior event your bool property will tell you whether any collisions happend to your game object or not. To make it more clear see unity Execcution order diagram - Unity - Manual: Order of execution for event functions (Scroll down to diagram).
As you can see all physics that include Collisions and Triggers events are processed before LateUpdate is called. if you are interested why not Update() … So one of the reason is that Update() you can use for you game logic. And the seccond i call it Magic or maybe i still didn’t understand all of the things :smiley:
For me that works perfectly!