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: [6385-výstřižek.png|6385]
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.

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 → Unity - Manual: Tags and Layers

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.

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);
}