• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Keegz · May 27, 2013 at 12:55 PM · c#rotationaxisnewlock

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);
     }
 }

}

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kajos · May 27, 2013 at 01:12 PM 0
Share

Why would you want to set the z roll of the camera in first instance?

avatar image Keegz · May 27, 2013 at 01:15 PM 0
Share

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.

avatar image Kajos · May 27, 2013 at 01:24 PM 0
Share

You could better focus on how not to set the z axis in first instance, ins$$anonymous$$d of resetting it I would think.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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; }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DanTaylor · Feb 11, 2020 at 08:50 AM 0
Share

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

avatar image
0

Answer by Stormizin · May 27, 2013 at 01:15 PM

Look at these links: "How would I lock a desired axis"

"How to set a single axis rotation of a gameobject"

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Lock Z Rotation. Character Controller. 1 Answer

Rotating a specific axis towards a object 3 Answers

rotate axis of movement 0 Answers

How to fix rotation while moving character 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges