- Home /
Rotating and moving a ball with a Trigger
I have a ball that is moving forward using addforce, and a disk that serves as a trigger. When the ball hits the trigger its y should rotate to match the disk's y rotation. That part works. Next it should move along its new forward. But instead it moves in a direction between the old and new direction.
I've included the code below, its really not much. As for objects is just a ball with a rigid body, and a disk(shrunken cylinder with a mesh colider) that has a rotation of (0,100,90)
var rot = 0;
function FixedUpdate() {
if(rot > 0){
transform.rotation.eulerAngles.y = rot;
rot = 0;
}
rigidbody.AddForce(transform.forward);
Debug.DrawLine(transform.position, -transform.forward * .5, Color.green, 60);
}
function OnTriggerEnter (col : Collider) {
var tran = col.transform;
rot = tran.rotation.eulerAngles.y - 90;
}
I'm guessing that you problem is that you are adding force to an already existing velocity. I'm not sure what the "right" way of dealing with this situation, but you might try zeroing out the velocity before adding the new force, depending on what you want to happen, just setting the velocity to the new direction and not add force at all.
See if this helps http://forum.unity3d.com/threads/39998-reset-rigidbody