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.

2 Answers

2

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?

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.