- Home /
Local 'forward'
Hi guys, I wondered if someone could confirm something for me. According to docs, transform.forward is forward in world space; what is it's local space equivalent? i.e. if I want an object to move in the direction of it's blue axis
no, forward is in fact "that thing's" forward.
the way it's nose is pointing. yes they are "local" as it were.
you also have up and right available...
file:///Applications/Unity/Unity.app/Contents/Documentation/Documentation/ScriptReference/Transform.html
At the start my objects Z axis is aligned with world z axis. It moves forward, hits the terrain which is in front of it, turns 90 degrees to the right (as expected), but then moves down the screen rather than moving 'ahead' along its Z axis (which is correctly pointed to the right edge of screen) - anything glaringly obvious? Thanks for the help!
public class BEHAVIOUR_NAVIGATION : $$anonymous$$onoBehaviour {
// Update is called once per frame void Update () { transform.Translate (transform.forward 2.0f Time.deltaTime, Space.Self); } void OnTriggerEnter (Collider other) {
if (other.tag == "TERRAIN") { Debug.Log ("Hit terrain"); transform.Rotate (Vector3.up * 90.0f); } }
}
Your problem is combining transform.Translate(xxx, Space.Self) with transform.forward. If you are using Space.Self then you want to use Vector3.forward as it is already doing that work for you.
Answer by majakthecoder · Apr 17, 2013 at 10:18 PM
Vector3 localForward = transform.worldToLocalMatrix.MultiplyVector(transform.forward);
is that what you are looking for?
And just as a reference - if you want to convert a direction it is much faster to use a Quaternion.
var localDirection = transform.rotation * someWorldSpaceDirection;
var localDirection = transform.InverseTransformDirection(someWorldSpaceDirection);
I believe this is due to the need to create the matrix - in any case:
The quaternion manipulation is 2x faster than the matrix method and the InverseTransformDirection is 8% faster than the quaternion.
Thank you, this is exactly what I was looking for. I also appreciate how you added how much faster each solution was :)
Ok, this one doesn't proyect a ray in local space, it projects in WORLD space. So, i was right. It is confusing.
//var localDirection = transform.InverseTransformDirection(transform.position.down);
This one either, again, it projects in WORLD space.
//var localDirection = transform.InverseTransformDirection(transform.position.down);
The CORRECT answer to this question, for my moment being is this:
CLEAR ANSWER to Local 'forward', without CONFUSION, slow or not:
Vector3 localForward = transform.worldToLocal$$anonymous$$atrix.$$anonymous$$ultiplyVector(transform.forward);
So, majathecoder was right. Sorry whydoidoit.
Can you please convert the code that majathecoder posted with the quaternion manipulation to a forward example? I believe your last solution is just confusing, if it is more optimized sorry but i dont understand someWorldSpaceDirection is. Again, it is confusing.
The code that majathecoder posted works as expected, slow or not it is usable.
Answer by hathol · Sep 12, 2012 at 04:21 PM
Just use the Vector3 constants for local space directions. (Vector3.forward, Vector3.up and Vector3.right)
Nope, that's forward in the objects local space :)
Vector3.forward applied to an object in world space (for example by using transform.position += Vector3.forward) will always move the object in the same direction relative to the origin, no matter the direction it is facing. Vector3 forward applied to an object in local space (for example by using transform.Translate(Vector3.forward)) will always move the object in the direction of its local forward axis
Now transform.forward does indeed give you the objects forward axis, but in world space coordinates (so relative to the origins axes). That's why all sorts of crazy things happen when you use transform.translate(transform.forward) since you're mixing coordinate systems :)
I think it's nomenclature between you guys:
a) $$anonymous$$ is saying - when in the context of a function that moves an item in local space use the Vector3.xxx constants
b) Fattie is saying - when you want to move an object relative to its local coordinate system add transform.xxx to its global position
Both are correct.
Personally I never use transform.Translate so transform.forward is the local equivalent, the blue Axis the OP asks about, when added to the transform.position of the object and Vector3.forward is the same when added to the transform.localPosition
Given info from Unite12 it appears that there is a significant cost penalty in using transform.position on objects inside a hierarchy because it is internally translated to transform.localPosition.
Exactly ;) A vector is just a vector. It depends how you use / interpret it. Since Transform.Translate takes a local space vector by default it's fine to use Vector3.forward.
The OP used a worldspace vector (transform.forward) as localspace vector which would of course lead to a strange behaviour ;)
It's like the "local position" of an object in it's own local-space is always (0,0,0)
is there a a way to read local forward values.
a game object is set to move on its own without any in put how would you read its movements relative to its facing potions.
It's wrong... Try transform.position += Vector3.forward to move a GameObject with rotation (0,-90,0). The "forward" will be in the X not the Z axis.
Answer by Caminhoneiro_Hell_Porpeta · Apr 15, 2019 at 03:56 PM
You can use the TransformDirection to use it as local. (:
Vector3 forward = obj1.transform.TransformDirection(Vector3.forward);
Using this line makes no sense at all as the result is exactly the same as just using
obj1.transform.forward
I've seen many people use TransformDirection with Vector3.forward which is just a complicated (and probably slower) way to write transform.forward
.
Is there a way to use transform.forward but for the left?
Answer by vladibo · Sep 21, 2016 at 12:47 PM
// point
internal static void ToLocal(ref Vector3 worldPoint, ref Quaternion worldRotation, ref Vector3 worldPosition, out Vector3 localPoint)
{
localPoint = Quaternion.Inverse(worldRotation)*(worldPoint - worldPosition);
}
internal static Vector3 ToLocal(ref Vector3 worldPoint, ref Quaternion worldRotation, ref Vector3 worldPosition)
{
return Quaternion.Inverse(worldRotation)*(worldPoint - worldPosition);
}
internal static Vector3 ToLocal(Vector3 worldPoint, Quaternion worldRotation, Vector3 worldPosition)
{
return Quaternion.Inverse(worldRotation)*(worldPoint - worldPosition);
}
internal static void ToWorld(ref Vector3 localPoint, ref Quaternion worldRotation, ref Vector3 worldPosition, out Vector3 worldPoint)
{
worldPoint = worldRotation * localPoint + worldPosition;
}
internal static Vector3 ToWorld(ref Vector3 localPoint, ref Quaternion worldRotation, ref Vector3 worldPosition)
{
return worldRotation * localPoint + worldPosition;
}
internal static Vector3 ToWorld(Vector3 localPoint, Quaternion worldRotation, Vector3 worldPosition)
{
return worldRotation * localPoint + worldPosition;
}
// vector
internal static void ToLocal(ref Vector3 worldVector, ref Quaternion worldRotation, out Vector3 localVector)
{
localVector = Quaternion.Inverse(worldRotation)*worldVector;
}
internal static Vector3 ToLocal(ref Vector3 worldVector, ref Quaternion worldRotation)
{
return Quaternion.Inverse(worldRotation)*worldVector;
}
internal static Vector3 ToLocal(Vector3 worldVector, Quaternion worldRotation)
{
return Quaternion.Inverse(worldRotation)*worldVector;
}
internal static void ToWorld(ref Vector3 localVector, ref Quaternion worldRotation, out Vector3 worldPoint)
{
worldPoint = worldRotation * localVector;
}
internal static Vector3 ToWorld(ref Vector3 localVector, ref Quaternion worldRotation)
{
return worldRotation * localVector;
}
internal static Vector3 ToWorld(Vector3 localVector, Quaternion worldRotation)
{
return worldRotation * localVector;
}
Your answer
Follow this Question
Related Questions
Rotate and transform in local space 2 Answers
Problem with bullet projectiles direction 2 Answers
Rigidbody constraints in local space? 7 Answers
How to Convert moveDirection.z = 20 to Local Space? 1 Answer