- Home /
Is there a way to lock my camera's rotation and movement on certain axis?
I have my camera attached to an object in my scene and it is parented to it. Therefore my camera follows the object in all directions and rotations. however I don't want this. I want my camera to be locked on the y position axis, and the x, and z rotation axis. Using c#.
Answer by Piflik · Jul 16, 2015 at 12:07 PM
Parent an empty to the object, not the camera itself, and in Update set the camera to use that empty's x and z positions.
[SerializeField] Transform target;
void Update () {
transform.position = new Vector3(target.position.x, transform.position.y, target.position.z);
}
The rotation is a bit more difficult, but you can calculate the necessary angle with a atan over the quotiont of the difference in the remaining axes.
e.g. if you want to only rotate along y, then the angle would be
float angle = Mathf.Atan((target.position.x - transform.position.x)/(target.position.z - transform.position.z)) //Possibly wrong sign, depending on your setup
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, angle, transform.rotation.eulerAngles.z);
Alternatively, you can use a lookRotation to create a rotation to look at your object. If the position of the camera is fixed as above, this should only rotate along the remaing axis.
transform.rotation = Quaternion.LookRotation(target.position - transform.position, Vector3.up);
hello, when I use this script:
float angle = atan((target.position.x - transform.position.x)/(target.position.z - transform.position.z)) //Possibly wrong sign, depending on your setup
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, angle, transform.rotation.eulerAngles.z);
it tells me the name atan does not exist in the current context.
I cant seem to figure out why, would you please help me?
Sorry, my bad. I started typing pseudocode before I noticed that it is nearly working C# and forgot to fix the first part. The function is part of the $$anonymous$$athf library.
$$anonymous$$athf.Atan(...
Thanks for all your help. However, this didn't quite give me the effect I was looking for in the rotation, but the
[SerializeField] Transform target;
void Update () {
transform.position = new Vector3(target.position.x, transform.position.y, target.position.z);
}
worked perfectly for locking the position like I wanted.
I ended up using transform.LookAt(target);
and setting an empty game object towards the front of my player and setting that as target and this works.
thanks again for all of your help.
Answer by KdRWaylander · Jul 16, 2015 at 11:33 AM
Hi,
Add a rigidbody to your cam, uncheck the 'Use Gravity' option unless what it will fall. Then, in 'Constraints', check all the axes (translation and/or rotation) you want to freeze :)
Hey Waylander!
Thanks for that extremely simple and elegant solution. I was looking for something like this....
I like to see a solution that doesn't always involve additional scripting. :)
Cheers!
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
move the object where camera look 0 Answers
How to make an object face the mouse in a non-2D world 1 Answer
RTS Camera movement wrong after rotation 1 Answer
Camera Rotation Question 1 Answer