- Home /
Lock rotation axis?
So I'm rotating an object around a sphere using the below code:
transform.LookAt(target2);
The problem is that I don't want it to rotate along the Z axis, only the X and Y axis. How would I do this?
Answer by Deikkan · Jul 10, 2010 at 04:50 PM
What you want it to do is keep the position of the target's z axis but only transform the x and y axis, like below.
transform.LookAt(Vector3(transform.position.x, transform.position.y, target2.position.z));
Oddly it doesn't seem to work. Its for the camera and I don't want it to rotate along the Z axis but that doesn't work basically at all. It doesn't look at the center of the sphere or anything.
you could also try 'transform.eulerAngles.z = 0;' That will make sure that the z rotation is always 0
@Deikkan - I think you want to look at Vector3(target.position.x, target.position.y, transform.position.z) to do what you are saying above. ie. project the target point into the horizontal plane containing the camera.
that transform.eulerAngles.z and x stuff works the best! i highly recommend you try that before you try the above
Answer by chrono1081 · Jun 18, 2011 at 03:08 AM
Thank you for this response! Its been a huge help to me.
Answer by Darren45 · Jul 26, 2013 at 08:09 AM
If it was me, I would use transform.RotateAround. transform.RotateAround (objectToRotateAround.transform.position, new Vector3 (1, 1, 0), turnSpeed * Time.deltaTime)
Answer by vd853 · Mar 31, 2015 at 04:27 PM
I used this to rotate a tank turret where the "up/down" rotation is locked. It is basically pointing at the player which is an airplane.
gameObject.transform.LookAt(new Vector3 (GameObject.FindGameObjectWithTag("Player").transform.position.x, transform.position.y,GameObject.FindGameObjectWithTag("Player").transform.position.z));
Your answer
Follow this Question
Related Questions
How to prevent Z axis rotation ? 1 Answer
How to make an enemy look at you and follow you 0 Answers
Rotation Jumping values (0 to 180) 1 Answer
how to tilt boat on turning 2 Answers
Angle to Rotate Vehicle in Scrolling in Environment 0 Answers