- Home /
Move object relative to its axis
Hello,
I have an object that gets rotated on the y axis during the game. I want it to move with WASD, but it only moves accordingly to the world axis and not accordingly to its own axis.
Any help?
if (Input.mousePosition.x < mouseArea)
{
cameraObject.position += Vector3(-MovingSpeed, 0, 0);
}
if (Input.mousePosition.x >= Screen.width - mouseArea)
{
cameraObject.position += Vector3(MovingSpeed, 0, 0);
}
if (Input.mousePosition.y < mouseArea)
{
cameraObject.position += Vector3(0, 0, -MovingSpeed);
}
if (Input.mousePosition.y > Screen.height - mouseArea)
{
cameraObject.position += Vector3(0, 0, MovingSpeed);
}
Have you tried localposition? transforms have 2 sets of values, local and world. Try local and see how it turns out.
Answer by JustFun · Aug 29, 2014 at 12:34 PM
Move it with Transform.Translate. The second parameter of this function defines if translation is relative to local or world coordinate system.
I give my vote to Transform.Translate(), but for the sake of completeness, you can use the transform.up, transform.right, and transform.forward for local movement:
if (Input.mousePosition.x < mouseArea)
{
cameraObject.position += transform.right * $$anonymous$$ovingSpeed;
}
if (Input.mousePosition.x >= Screen.width - mouseArea)
{
cameraObject.position -= transform.right * $$anonymous$$ovingSpeed;
}
if (Input.mousePosition.y < mouseArea)
{
cameraObject.position -= transform.forward * $$anonymous$$ovingSpeed;
}
if (Input.mousePosition.y > Screen.height - mouseArea)
{
cameraObject.position += transform.forward * $$anonymous$$ovingSpeed;
}
Answer by superdudeman · Aug 29, 2014 at 01:08 PM
An alternative to using Transform.Translate, something like:
if (Input.mousePosition.x < mouseArea)
{
cameraObject.position += MovingSpeed * -cameraObject.right;
}
transform.right is the Vector facing right from where the object is facing, same concept with transform.up and transform.forward
Your answer
Follow this Question
Related Questions
an ignorant's trigonometry woes: rotate a transform around an axis 2 Answers
bone rotations changed in 4.3.2 inspector? 0 Answers
Why does the object alignment only work when the object isn't rotated? 2 Answers
simple rotation question c# local/world 2 Answers
Quaternion.Slerp on local axis 1 Answer