- Home /
how to lock the z axis rotation
Hi I am somewhat new to Unity and I have a problem, I want to lock all rotation movement for my camera, I was given a script that made it possible for me to lock the x and y axis but not the z please can you help me. Here is the script.
using UnityEngine;
public class Camera_FPS : MonoBehaviour { public Vector2 rotationLimit; //Limit for the vertical rotation of the camera. X is higher limit, Y is lower limit. public float rotationSpeed = 600.0f; //Vertical rotation speed of the camera public bool invertY = true; //Invert vertical rotation public bool headBobEnabled = true; //Enables/disables head bobbing public Vector2 headBobDistance = new Vector2(0.2f, 0.3f); //The distance the camera travels in the y-axis for the headBobbing effect public float headBobSpeed = 5.0f; //Speed at which the player's head bobs
private Vector3 initialLocalPos; //Starting local y-position of camera
private float rot; //Stores the rotation value incremented by mouse movement in the y-axis
private Transform t; //Reference to the transform component of the game object
public void Awake()
{
t = transform; //Cache transform component
initialLocalPos = t.localPosition; //Store initial local position of the camera
}
public void Update ()
{
//Update rotation
rot += Input.GetAxis("Mouse Y") * rotationSpeed * ((invertY) ? -1 : 1) * Time.deltaTime;
if(rot < rotationLimit.y) //Check for lower limit
rot = rotationLimit.y;
if(rot > rotationLimit.x) //Check for upper limi
rot = rotationLimit.x;
t.localRotation = Quaternion.AngleAxis (rot, Vector3.right); //Apply rotation to transform
//Head bobbing
if(headBobEnabled)
{
//Calculate percentage of blend between stationary and bobbing camera positions
float percentage = Mathf.Min(1, Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal")));
//Calculate desired x position
float desiredPosX = initialLocalPos.x + headBobDistance.x * Mathf.Sin(Time.time * headBobSpeed + Mathf.PI/2);
//Blend between stationary and desired x position
float newX = (initialLocalPos.x * (1 - percentage)) + (desiredPosX * (percentage));
//Calculate desired y position
float desiredPosY = initialLocalPos.y + headBobDistance.y * Mathf.Sin(Time.time * 2 * headBobSpeed);
//Blend between stationary and desired y position
float newY = (initialLocalPos.y * (1 - percentage)) + (desiredPosY * (percentage));
t.localPosition = new Vector3( newX, newY, t.localPosition.z);
}
}
}
Why would you want to set the z roll of the camera in first instance?
I want to make sure that when the player moves the cursor that there is no rotation, so that it just stays on a fixed location.
You could better focus on how not to set the z axis in first instance, ins$$anonymous$$d of resetting it I would think.
Answer by dhurstdev · May 07, 2016 at 05:53 PM
Everyone... Much easier than locking an axis... I have wasted too much time on this, trying to calculate the angle and solve the BS... Apparently Quaternion.LookRotation, does this without having to work about correcting angles, or flipping! Amazing! http://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html Hope this helps: private void SetRotationFromWorldPoint(Transform trans, Vector3 worldPoint) { Quaternion currentRotation = trans.rotation; currentRotation = Quaternion.LookRotation(worldPoint); trans.rotation = currentRotation; }
Nice one! I was able to lock the Z rotation of an object by using...
orientation = Quaternion.LookRotation(leftHand.PointerPose.forward, Vector3.up);
Combining the Forward vector of the object I was tracking and the Up vector for the world. Easy! :D
Answer by Stormizin · May 27, 2013 at 01:15 PM
Look at these links: "How would I lock a desired axis"