- Home /
Trigger doesnt do Speed Booster
Hi everyone, i want that my ball get a speed booster when it crosses an object. For that i did this Script, but when the ball cross the box nothing happens. The box has rigidbody and default box collider. I probe with trigger off and trigger on. And i dont know how to do. Any idea? Thanks.
var ballForce:uint = 40;
function OnTriggerEnter(other:Collider)
{
if(other.gameObject.tag == "Ball")
{
var pelota:GameObject = other.gameObject;
pelota.rigidbody.AddForce(0,0, (-1)*ballForce);
}
}
Note that the trigger box does not require a rigidbody, only a collision box. That said, make sure that your ball has a rigidbody, a collision box, and it's tag is "Ball". Also, make sure that your script is attached to the trigger in the editor.
Answer by NoahConstable · Mar 03, 2014 at 11:06 PM
Hey AnxoDL, I used your script in my project and it worked. Here is what I did:
Change line 8 from:
pelota.rigidbody.AddForce(0,0, (-1)*ballForce);
To:
pelota.rigidbody.AddForce(-Vector3.forward*ballForce);
Now keep in mind, I'm assuming that your "AddForce(0, 0, (-1)*ballForce)" means you want the object to move back on the "Negative of the z-axis" upon impact with the trigger. If not change the keyword "-Vector3.forward" to the direction you want. If you need to know more, here is a link to the documentation on how to use Vector3 in AddForce:
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html
Also note 40, as an interger for how fast you move, is pretty small. I had to raise the number from 40 to 400 to see a difference. If you have any questions just comment on this. If this fixed your problem, please mark it as solved so others may see it, and ask there own questions to grow the Unity community.
Your answer

Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Help on selecting and unselecting a gameobject? 2 Answers
What variables can i declare? 1 Answer
Array problem -3 Answers