- Home /
Convert local forward into world vector
I have a object that travels from point a to point b. I was using transform.LookAt() and then transform.forward to get the correct direction to move the object in the desired direction.
Now I need to rotate that object, but keep it always moving in the same direction. Obviously when the object rotates it constantly has a new transform.forward which is no longer the desired direction I want it to travel.
So what I was thinking is i could take the transform.forward of the object before it starts rotating, then convert that vector3 into a world vector instead of local. Then the object can move in the correct direction based on world coordinates instead of local coordinates while it's rotating.
Will this work? How would I conver the local vector into a world vector? (i tried TransformDirection and it didn't seem to work). Is there a better way of getting an object to move in a specific direction while it rotates? How?
Transform.TransformDirection(Vector3.forward) is equivalent to Transform.forward.
Answer by Jessy · Apr 26, 2013 at 06:36 PM
Transform.forward is in world space. Just store that Vector3 before you rotate the object.
I actually tried that before I asked this question. It does the same thing as using current transform.forward. I'll try it again and see what happens.
with that the object still just moves forward based on its rotation and not based on the rotation that it started out at. basically I got something like this:
var forward :Vector3;
var rotation :Vector3;
var speed :float;
var location :Vector3;
function Start()
{
transform.LookAt(location);
forward = transform.forward;
}
function Update()
{
transform.Rotate(rotation);
transform.Translate(forward*speed*Time.deltaTime);
}
Your code fails because you're not using the second parameter of Transform.Translate. You need to do a better job of reading the documentation, as far as vector spaces go.
Your answer
Follow this Question
Related Questions
Help to figure a logic to achieve a movement. 2 Answers
Unable to fully rotate GameObject on tap 2 Answers
Having Trouble with Vector3.Angle 1 Answer
Get rotation from hit object 1 Answer
Workaround for Quaternion.eulerAngles 360 Degree Limit? 1 Answer