rigidbody.Addforce stacking?
Hi, I have an object moving and I want it to jump whenever it collides with object named "red". This is the script
var jumpheight : float = 800;
function OnCollisionEnter(collision : Collision) {
if(collision.collider.name=="Red")
{
rigidbody.AddForce(Vector3.up * jumpheight);
}
}
It works the first time it colides with this object but the second time it jumps like 2 times higher than it should. Then it works normally again and later on it jumps 2 times higher again.
EDIT: This is how the scene looks from a side. When the ball touches the red block the script makes it jump.
umm, Strange thing just happend, I tried removing the problematic blocks and copied the ones that work, moved them and they seem to work. When I remove them and create them again, they work o.O What the heck is going on?
Ok, I think I finally know why its happening but I will need help on fixing this thing. I think it is like this: When the sphere moves on the blue line, it jumps normally but when it moves on the red line, so its floating in the air for a second, then it will jump a lot higher for some reason.
Are you sure it's always on the second occasion?
Could it be from colliding a second time before the jump has moved it clear?
Answer by Karsnen_2 · Jan 04, 2013 at 02:31 PM
RGI - >
I hope that there is no other script which is influencing your object. I would suggest you to use "tag" instead of "name". If you are not aware of it, you could find it here -> http://docs.unity3d.com/Documentation/Components/class-TagManager.html
Now tag the collider and check for collision like this,
function OnCollisionEnter(collision : Collision)
{
if(collision.collider.tag == "Enemy")
rigidbody.AddForce(Vector3.up * jumpheight);
}
Now try to run it. It should work.
If it does not work, then you need to record events and see where the problem is. For example,
function OnCollisionEnter(collision : Collision)
{
if(collision.collider.tag == "Enemy")
{
rigidbody.AddForce(Vector3.up * jumpheight);
Debug.log("Contact with the collider");
}
}
function OnCollisionExit(collision : Collision)
{
if(collision.collider.tag == "Enemy")
Debug.log("Contact with the collider - EXITED");
}
By doing this way, you could know what is happening inside. For example if you object is exits the collider and then enters the colliders once again then force is being applied twice. I can only speculate from here, but if you more help let me know.
cheers!!!
P.S. : I am not good in UnityScript, beware you might get syntax errors.
Did not help, and I dont see any way how this could have solved the problem..
The part about adding a print in collisionEnter would definitely help. You'd see right away if the double jumps were caused by double hits. Even better, print "hit "+ collision.transform.name
. Sometimes that can pick up hitting stuff you never even thought of.
The part about tag vs. name. Sure, good habit, but nothing to do w/the question. And even then, CompareTag
is the official way (I think it runs a hash, so is a little faster than ==. And gives helpful errors if you misspell the tag name.)
The tag vs name thing is just a good habit. I am pretty sure RGI that it would help to find where the problem is. I did mention that, it would help to find what is happening inside and not giving you soln. We can't provide solution here, we can only help you to figure out the solution.
Answer by Lovrenc · Jan 04, 2013 at 02:27 PM
As MarkFinn said. Your character is coliding while IN JUMP and is adding force. What you should do is start using states (walking, jumping, ...).
When your character is in jumping state you ignore collisions and dont add force.
eg.
if(collision.collider.name=="Red" && currentState != States.Jumping)
{
rigidbody.AddForce(Vector3.up * jumpheight);
}
Your answer
Follow this Question
Related Questions
Having a OnTriggerEnter make another gameobject Add Force in UnityScript. 1 Answer
Moving rigidbody with addforce, velocity or position causes another object not to collide anymore. 0 Answers
Collider seems bigger than it is 0 Answers
Unity 5: AddForce Increases power when already being pushed towards a collider. How to make stop? 1 Answer
OnCollisionEnter returning incorrect collision points 0 Answers