- Home /
Add a force to a ball using the Boxcolliders AXIS
Hello, im pretty new to this, I have googled and tried so many ways, but I cant figure it out.
This is the code I have:
function OnTriggerEnter (other : Collider) {
ball = GameObject.FindWithTag ("Player");
ball.rigidbody.AddForce (-1000, 0, 0);
}
this actually works. as long as ball does not rotate the axis X Y.
My questions are: 1- can I use the local axis from the box collider (in this case I want Y local) to say, Ball you will get force from the Y axis of the boxcollider. I have tried relativeForce and without success. 2- even switching off the collider, the force still exists even though I added only on the onTriggerEnter. why?
many thanks in advance.
Answer by robertbu · Sep 23, 2014 at 06:03 PM
If I've puzzled through your setup correctly, you want:
ball.rigidbody.AddForce (transform.up * 1000, 0, 0);
I assume the above code is on the fan. So 'transform.up' will be the local 'up' vector of the game object the script above is on (i.e. the fan). So the above code should add force as you describe.
Did you try the code? If so, what is the behavior?
Answer by rip3rs · Sep 23, 2014 at 09:23 PM
I was doing it wrong,
I feel so embarassed...
function OnTriggerEnter (other : Collider) {
if (other.tag.Equals("Player")) {
other.rigidbody.AddForce (-1000, 0, 0);
}
}
works like a charm. best.
Your answer
Follow this Question
Related Questions
Object Problems with colliders? 1 Answer
I am trying to have a ball roll on a line that was drawn from a Line Renderer 1 Answer
How can I calculate the force required to contradict gravity?2d 2 Answers
Box Colliders of imported OBJ not centered on OBJ, How would i center it? 2 Answers
why isn't my OnTriggerEnter2D() function working? 14 Answers