- Home /
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.
I'm not sure if I understand it right so I'll put this as a comment But shouldn't this solve it? void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag.Equals ("platform")) {
moveForward = true;
} else{
moveForward = false;
}
}
This would only trigger upon first contact with a collider. Unfortunately, this doesn't help when the goal is to deter$$anonymous$$e when there are no active collisions with any objects during any given frame.
But if collider is not available at that place then OnCollisionEnter will not be called. and at that time i want to give gravity to object.
Answer by Eno-Khaon · Jul 29, 2015 at 10:20 AM
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 collider is not available when object is grounded then this OnCollisionStay or OnCollisionExit will be called ?
No , collision methods wont be called but boolean will maintain the states
This solution will not work . $$anonymous$$y object will be in air while jumping and at that time isGrounded will be false. and it will fall down.
I want to check at animation end object is colliding to the ground or not ?if not then how can i detect that ?
yes it would be false, but if it collides then it will start moving forward else it would keep falling down. Thats what needed i guess.
$$anonymous$$aybe you need to specify more to make others understand what exactly is needed .
Answer by mightyBearArthur · Sep 27, 2016 at 06:15 PM
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 - https://docs.unity3d.com/Manual/ExecutionOrder.html (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 :D For me that works perfectly!