- Home /
Strange movement behavior
I just created a small projectile for my game and added a little script to control its movement. It is a really easy script, but it behaves incorrectly.
var projSpeed : int = 1;
function FixedUpdate() {
if(statics.turnActive) {
transform.Translate(transform.right * projSpeed);
}
}
(turnActive is set in another script, based on player input)
Now it does move the projectile, but when I rotate the projectile model 90° in the scene view, the movement direction turns 180°. I have no idea why it should do that. I can change the direction 90° by using transform.forward instead of transform.right, obviously, but that doesn't solve the problem. While some enemies will fire in the correct direction, others will fire up to 90° to the left/right, or even backwards.
Tried an even simpler script, same behavior. The movement direction changes twice the amount of the object's rotation.
function Update () {
transform.Translate(transform.forward);
}
Answer by Piflik · Sep 17, 2011 at 07:57 PM
Ok...I am stupid...got it now.
transform.forward creates a vector in world coordinates, so using that in a local reference system applies the rotation twice.
Simple solution: Either use Vector3.forward, or Space.World as reference system.