- Home /
Trying to make a Projectile Rotate and Move Forward..
I'm trying to make a projectile move forward and rotate as it does so.
Here is the code I currently have for the projectile's movement:
function Update () {
if(travelRight){
transform.Translate(Vector3(-speed * Time.deltaTime * 2, 0, 0));
transform.Rotate(Vector3(0, 0, 20));
Destroy(gameObject, 3);
}
if(!travelRight){
transform.Translate(Vector3(speed * Time.deltaTime * 2, 0, 0));
Destroy(gameObject, 3);
}
}
In the "if(travelRight){" statement, the projectile just instantiates and rotates in place.
Whereas in the "if(!travelRight){" statement, the object obviously just moves to the left.
I want the projectile to travel right or left, but rotate as it does so, and if I add any rotate function (so far) the projectile rotates and doesn't move forward. And if it does make any movement forward, it's not a local rotation. It ends up being a circular, global-like rotation. Does anybody know how to fix this? I've tried so many things and keep failing :( any help would be greatly appreciated!! Thanks
it's a rigidbody, btw.
EDIT: I have since just made a sprite to fake the look of rotation, but knowing this information would still be useful. Thanks!
Have you tried: transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
the transform.Translate / Rotate would act differently depending to what co-ordinate system you want them to translate / rotate against.. just wondering if you might already have tried Space.world as a parameter.
Another fyi: if 'travelRight' becomes true even for a single frame, since its running on a "UPDATE" call, your translate will be all screwed. So make sure, when an object has started moving in one direction, keep it moving right till it reaches its destination.
tried transform.Rotate(Vector3.up * Time.deltaTime, Space.World); and it didn't do anything. It might be the moving right problem you mentioned, but I have no idea how to fix that :( what do i have to write to say "keep moving til you reach the destination"?
You need a trigger at the start and destination, which is either set off/reset by collision detection or Input, and the setting of the "travelRight" should happen outside the Update function, only ONCE, not in every frame. travelRight is a even monitor in this case, for what its doing.. and events trigger once, and then something has to reset it.
Answer by MadMapp · Nov 05, 2013 at 08:49 PM
var X: int;
var Y:int;
var Z: int;
var rotationsPerMinute : float = 10.0;
function Update()
{
transform.Rotate(X,Y,Z*rotationsPerMinute*Time.deltaTime,0);
}
attatch this to the object you want to spin, have a second object an empty, atatch a script to make it move on instantiation, attatch you rotating object as a child object, what your doing presently is rotating while moving forward which will produce a circle