Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 ItsKhanny · Jan 12, 2016 at 03:40 AM · c#clamped rotation

How to clamp rotation

I have a script to allow the camera to move up and down, but the problem It will go in a complete circle if I move the mouse all the way up or down. How do I stop it from going around and limit it to looking almost completely down to looking completely up.

Script C#

public GameObject target; public float rotateSpeed = 5; Vector3 offset;

 // Update is called once per frame
 void Update () {
         
     Vector3 rot = transform.eulerAngles;
     
     float vertical = Input.GetAxis ("Mouse Y") * rotateSpeed;
     target.transform.Rotate (-vertical, 0, 0);
     
     float desiredAngle = target.transform.eulerAngles.y;
     Quaternion rotation = Quaternion.Euler (desiredAngle, 0, 0);
     transform.position = target.transform.position - (rotation * offset);
     rot.x = Mathf.Clamp(rot.x + -45, 0, 0);
     //transform.eulerAngles = rot;
     
     
     transform.LookAt (target.transform);
         

 }
Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by mrmcduck · Jan 12, 2016 at 04:57 PM

First of all I'd like to say that this is the MouseLook.cs file from the Unity Standard Assets FirstPersonController. It does exactly what you are looking for (besides allowing the camera to look in all directions which can be easily changed). It should be easy to adapt to your use.

LookRotation() is called at an Update() method of any script attached to the gameobject the camera is attached to, with the following parameters: 1. Transform of the gameObject, the camera is attached. 2. Transform of the main camera (Camera.main.transform).

(If you decide to use the script do not forget to call the Init() at the Start method.)

 public class MouseLook
     {
         public float XSensitivity = 2f;
         public float YSensitivity = 2f;
         public bool clampVerticalRotation = true;
         public float MinimumX = -90F;
         public float MaximumX = 90F;
         public bool smooth;
         public float smoothTime = 5f;
 
         private Quaternion m_CharacterTargetRot;
         private Quaternion m_CameraTargetRot;
 
         public void Init(Transform character, Transform camera)
         {
             m_CharacterTargetRot = character.localRotation;
             m_CameraTargetRot = camera.localRotation;
         }
 
         public void LookRotation(Transform character, Transform camera)
         {
             float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
             float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;
 
             m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
             m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
 
             if(clampVerticalRotation)
                 m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);
 
             if(smooth)
             {
                 character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot, smoothTime * Time.deltaTime);
                 camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot, smoothTime * Time.deltaTime);
             }
             else
             {
                 character.localRotation = m_CharacterTargetRot;
                 camera.localRotation = m_CameraTargetRot;
             }
         }
 
         Quaternion ClampRotationAroundXAxis(Quaternion q)
         {
             q.x /= q.w;
             q.y /= q.w;
             q.z /= q.w;
             q.w = 1.0f;
 
             float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
             angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
             q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
 
             return q;
         }
     }




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 ItsKhanny · Jan 12, 2016 at 06:50 PM 0
Share

I would use this, but I have my event scripts to disable the script during cut scenes and with this script it wont work.

avatar image
0

Answer by S_Byrnes · Jan 12, 2016 at 04:45 AM

I'm new to this as well, but I did something similar yesterday, try replacing your rot.x = Mathf.Clamp(rot.x + -45, 0, 0); with:

 rot.x = Mathf.Clamp(rot.x, -20, 45);

Where -20 is the lowest it can go and 45 is the highest.

Let me know how you go, keep in mind I'm no expert, just thought I'd see if this helps you.

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 ItsKhanny · Jan 12, 2016 at 06:51 PM 0
Share

It did not work, it ignores the clamp and so when I look, it does the same thing

avatar image
0

Answer by Eno-Khaon · Jan 12, 2016 at 08:02 PM

Another approach you can take is to consider a level baseline and use a float to keep track of your current rotation. Why? Well, there are two reasons to consider this approach:

1) Upper and lower limits can be maintained and enforced at any angle (with proper accommodation for a "current" facing)

2) You aren't subject to Gimbal Lock when looking beyond straight up or down

Here's a basic idea of what would need to be implemented (though this probably won't work on its own):

 // C#
 public float minYRotation = -70.0f;
 public float maxYRotation = 92.0f; // For proof of concept, basically
 float currentRotation = 0.0f;
 Quaternion yRotation;
 
 void Update()
 {
     currentRotation += Input.GetAxisRaw("Mouse Y");
     currentRotation = Mathf.Clamp(currentRotation, minYRotation, maxYRotation);
     transform.Rotation = Quaternion.identity * Quaternion.AngleAxis(currentRotation, transform.right);
 }

From this approach, the current Y rotation is maintained by a single float value and is sent through a rotation to determine a new facing angle. Adding the rotation to Quaternion.identity ensures that looking up and down will never exceed the limits placed on it.

In this specific example, the X axis will also need to be calculated manually in the same way, but there will likely be no need to clamp the values for it.

Comment
Add comment · Show 2 · 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 ItsKhanny · Jan 13, 2016 at 03:18 PM 0
Share

It works, but when the camera hits the limit, it starts glitching out and is there a way to change the mouse sensitivity so the camera moves faster

avatar image Eno-Khaon ItsKhanny · Jan 13, 2016 at 06:13 PM 0
Share

Sensitivity's actually quite simple:

 public float mouseSensitivity = 1.0f; // Set this as desired
 
 // ...
 
 currentRotation += Input.GetAxisRaw("$$anonymous$$ouse Y") * mouseSensitivity;

As for the camera having issues when you reach the limit, I'm not really sure what the problem there would be quite yet, sorry to say.

avatar image
0

Answer by Lairex59 · May 02, 2020 at 12:36 PM

Just by the way here it's explained well: https://www.youtube.com/watch?v=JeF0hoJWLz4

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

55 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

How to clamp rotation between negative and positive value 2 Answers

Another Mathf.Clamp Not Working on Rotate? 1 Answer


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