- Home /
Can't get object to move and rotate on its own axis
Hello, I'm having some trouble getting an object to move and rotate on its own axis. Here's my code
// Moving works the way I want it to
transform.position = transform.TransformDirection(Vector3(moveX, moveY, moveZ));
// But it rotates around the world origin. But if I comment out the first line it rotates locally
transform.eulerAngles = Vector3(rotX, rotY, rotZ);
I've also tried using localEulerAngles instead but that still doesn't do anything
Answer by sparkzbarca · Oct 13, 2012 at 07:12 AM
transform.rotation(vector3 rotation) its default space is self, that is it rotates relative to self.
dont forget to mark as answered!
Thanks for trying to help but that didn't really answer my problem.
eulerAngles works perfectly on its own. transform.TransformDirection changes the axis to the origin and I can't figure out how to get my object to rotate around it's own axis without screwing up the movement.
Answer by gribbly · Oct 13, 2012 at 07:40 AM
Us the Rotate method:
float speed = 1.0f;
//this will rotate around the local y axis
transform.Rotate(0, Time.deltaTime * speed , 0, Space.Self);
I tried using Rotate but it still rotated around the origin. I need to use eulerAngles though because I'm controlling the amount of rotation with a slider.
I ended up doing something different, thanks for the help though