- Home /
How to convert one axis to world space?
I'm trying to make my character move relative to the rotation. The issue with that is that when I realized rotating the character alone didn't cause the movement controlls to move the character in that direction, I had to use TransformDirection(); The problem with that function is that if I look up, the y axis on the world space is now what would be backpedalling, so you end up floating into space.
I could make a new vector each frame that only carries the x change and 0 0 on y and z, but that seems very inefficient; I'm sure there is a better way to make the character move relative to their rotation.
positionChange.z = Time.deltaTime * zSpeed;
positionChange.x = Time.deltaTime * xSpeed;
//this vector's transform z direction should change but no others.
// Or maybe x, I try both until it works to be fair.
controller.Move(transform.TransformDirection(positionChange));
Answer by xthunderduckx · Mar 30, 2019 at 03:49 AM
Fixed it by creating a second vector3, so instead of creating a new vector each time it's just using two different ones.
You can use transform.forward
to get the normalized direction that a transform is facing. You can then multiply that by your walking/running speed * delta time, optionally zeroing out the y value if that’s handled some other way by the controller.