- Home /
A problem with relative velocity
Vector3 relativeVelocity = transform.InverseTransformDirection(new Vector3(move * maxSpeed, rigidbody.velocity.y, 0));
rigidbody.velocity = relativeVelocity;
This should move object in it's local x axis but it moves in global. Why?
Answer by Owen-Reynolds · Nov 22, 2014 at 04:00 PM
Well, and you knew this was coming, it doesn't work because your math is wrong. Local space vs. World space is tricky.
Your input, Vector3(move*max, oldY,0)
is in local space. That's a lie, since it's just numbers. But it's intended to be used as local -- you want to say "move on MY x." Almost all movement inputs are in local space, usually (0,0,speed)
to move the way you're facing.
InverseTransformDirection
converts World to Local. So, your ITD has the wrong type of input. What ITD is good for, is taking your current velocity (which is always world,) converting to local, and then checking if you're moving more left than right, etc... . This isn't done very often (since it hurts your head.)
The standard movement is, spelled out:
relativeMove = new Vector3(xMove, 0, zMove);
velocity=transform.TranformDirection(relativeMove);
As a check: relativeMove is considered to be local. TransformDir converts local to world. velocity always expects world.
Object should move only in two dimensions relative to the local space.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Error in scripting (HeadShot code) 1 Answer
How to find child object, without name or tag. 2 Answers
Parent object to child of another? 1 Answer