Problem with gravity, OnCollisionEnter and OnCollisionExit
Description: I want my gameObject to rotate by 90 degrees when it collides with wall. After that i want to turn off gravity so it can go upwards. When it gets off the wall i want to reset rotation and turn gravaty back on. Problem: When I use OnCollisionEnter it works (rotates gameObject and turns off gravity) but when I add OnCollisionExit (to reset rotation and turn gravity on when there is no more wall) gravity dosen't turn off in OnCollisionEnter function it stays On always. Note that OnCollisionEnter works when OnCollisionExit isn't added in script.
Here is my code
void OnCollisionEnter(Collision col) { if (col.gameObject.tag == "Colli") {
Vector3 rotatY = new Vector3(-90, 0, 0);
transform.Rotate(rotatY);
rb.useGravity = false;
}
}
void OnCollisionExit(Collision other)
{
Vector3 resetRot = new Vector3(0, 0, 0);
transform.Rotate(resetRot);
rb.useGravity = true;
}
Answer by josehzz112 · Sep 10, 2016 at 12:01 AM
Thats because you are not testing if you exited the wall. Change OnCollisionExit()
to this:
void OnCollisionExit(Collision other)
{
if(other.gameObject.tag == "Colli")
{
Vector3 resetRot = new Vector3(0, 0, 0);
transform.Rotate(resetRot);
rb.useGravity = true;
}
}
Edit: I just added Debug.Log in OnCollisionEnter and OnCollisionExit and it says that both functions are working. But in reality is not wokring how it should. $$anonymous$$aybe functions are activating to quick or something? $$anonymous$$aybe when OnCollisionEnter rotates gameObject it translates it a bit so OnCollisionExit is automatickly fired so I get illusion that it isn't working? But if that is so it would still need to reset transform.Rotation to original but it dosen't. Any idea?
I hope you guys understand what I mean since english is not my first language. Cheers
I guessed right. It transforms position of my object so OnCollisionExit is automaticly fired after OnCollisionEnter. Thanks @josehzz112 you answered my question and it works, almost. But how do I fix that problem with translation? I'll upvote your answer since it does what I asked first time.
I don't quite understand what's your new problem, when you do transform.Rotate() it moves the GameObject? its the object not a square? @Thevale Could you please elaborate more..
So, new problem is that when it collides with wall it rotates gameObject by -90 degrees and it turns off gravity. And when that funcion (OnCollisionEnter) is executed it automaticly executes next funcion (OnCollisionExit) witch rotates object back by 90 degrees and turns gravity on. Becouse of that my game object is always rotating and turning gravity on and off.
What I want from my code is to walk on wall and rotate it by 90 degrees depending on current rotation and wall rotation.
It seens like when the GameObject rotates because of OnCollisionEnter
the result rotation makes the GameObject to be outside of the wall so it executes OnCollisionExit
.
I don't know how its your set up, but I would consider not turning off gravity when OnCollisionEnter
happens.
Also I think this can be ask as a new question, try that, maybe someone else knows how to solve it.
Answer by akshay_zz · Sep 10, 2016 at 05:04 PM
//May this will work for you void OnCollisionEnter(Collision col) { if (col.gameObject.tag == "Colli") {
Vector3 rotatY = new Vector3(-90, 0, 0);
transform.Rotate(rotatY);
rb.useGravity = false;
}
else { Vector3 resetRot = new Vector3(0, 0, 0); transform.Rotate(resetRot); rb.useGravity = true; }
}
Your answer
Follow this Question
Related Questions
After added a rigidbody in a GameObject, it moves on rotation 2 Answers
Why does the player not move around planet with gravity ? 1 Answer
Why is my Rigidbody freaking out like this? 0 Answers
Need help with collision Detection 0 Answers
Why does my rigidbody move it's transform while rotating? 1 Answer