- Home /
Move relative to objects rotation (C#)
How would you make an object move based on the objects rotation, I have this:
if(engineReady){
float tempPitch = pitch / 1.66666f;
tempPitch = (tempPitch - 30.0f) / 100.0f;
pitchDegrees = tempPitch;
heliSpeed = Mathf.Abs((tempPitch * 100.0f));
heli.transform.rotation = new Quaternion(pitchDegrees, heli.transform.rotation.y, heli.transform.rotation.z, heli.transform.rotation.w);
if(curHeight < 0.01){ heliSpeed = 0.0f; pitch = 50; }
if(pitch > 50){
heli.transform.position += Vector3.forward * (heliSpeed * Time.deltaTime);
} else {
heli.transform.position -= Vector3.forward * (heliSpeed * Time.deltaTime);
}
}
// ===== Yaw ======= //
if(Input.GetKey(KeyCode.Z)){
if(engineReady){
heli.transform.rotation = new Quaternion(heli.transform.rotation.x, heli.transform.rotation.y + 0.05f, heli.transform.rotation.z, heli.transform.rotation.w);
}
}
if(Input.GetKey(KeyCode.C)){
if(engineReady){
heli.transform.rotation = new Quaternion(heli.transform.rotation.x, heli.transform.rotation.y - 0.05f, heli.transform.rotation.z, heli.transform.rotation.w);
}
}
But .forward always follows the global Z axis.
Answer by clunk47 · Oct 10, 2013 at 01:24 AM
Just filling in the OP with more info: transform.right
, transform.forward
and transform.up
are your object's local standard basis vectors (they are also unit
vectors, meaning they have a length of 1) (Btw if you're not familiar with standard basis vectors, they're actually the i
along the x and j
the y in a 2d coord Cartesian system). Those vectors will always rotate along with your object. Think of transform.forward
as a vector pointing out of your chest (or your mouth, if you wanna stick to the head), transform.up
out of your head and transform.right
out of your right ear :D
Vector3.up
, Vector3.forward
and Vector3.right
however work independently of your object - they're global to the world. Imagine standing in a room's corner such that at first both your local and the room's global axis are aligned, if you rotate, your local axis will rotate with you, but the room's won't :D
Just to add, if you want to use back, you would use -tranform.forward, left will be -transform.right, down will be -transform.up. You can get the same results with TransformDirection.
transform.TransformDirection(Vector3.forward);
transform.TransformDirection(Vector3.back);
//and so on...
Correct. It's also interesting to mention that the docs actually got it wrong. (which is typical)
Transforms direction from local space to world space.
It's actually the opposite, TransformDirection
transforms a Vector from global to local space. (They actually gave a correct example with cameraRelativeRight
, so they kinda said something but did otherwise) I just tested it out, create an empty scene, with a cube and move it with:
void Update()
{
void Update()
{
float h = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
float v = Input.GetAxis("Vertical") * speed * Time.deltaTime;
// _transform.localPosition += _transform.right * h;
// _transform.localPosition += _transform.forward * v;
Vector3 RIGHT = _transform.TransformDirection(Vector3.right);
Vector3 FORWARD = _transform.TransformDirection(Vector3.forward);
_transform.localPosition += RIGHT * h;
_transform.localPosition += FORWARD * v;
}
}
RIGHT
and FORWARD
are exactly the same as transform.right
and transform.forward
. Both will move the cube relative to its local axis.
And since that doc is blown, same is true for the opposite.