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 justantorami · Feb 28, 2019 at 06:53 PM · rotationscripting problemprogrammingeulerangles

Limit Horizontal Rotation

I have this function, that rotates the object and the camera associated with the object moving the mouse nicely.


My problem is that the clamp isn't working as intended, I have been trying a few things I found searching but none of them seems to work.


I want the rotation to be clamped betweent -40 and 40 (degrees) but the Clamp is clamping it between 0 and 40 because as soon as the object rotates to <0 degrees, it's actually 359º with is bigger than 40º so it moves back to 40 degrees.


    private void Look()
     {
         _mouseX = Input.GetAxis("Mouse X");
         _mouseY = Input.GetAxis("Mouse Y");
         Vector3 newRotation = transform.localEulerAngles;
 
         newRotation.y += _mouseX * _sensibility * Time.deltaTime;
         newRotation.x += -_mouseY * _sensibility * Time.deltaTime;
 
         newRotation.y = Mathf.Clamp(newRotation.y, -40f, 40f);
 
         transform.localEulerAngles = newRotation;
     }


Thank you :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by WarmedxMints · Feb 28, 2019 at 07:07 PM

I posted this for someone earlier today. It is easier to handle the rotation on two transforms. So make your camera a child of another gameobject and attach the script to the parent.

 using UnityEngine;
 
 public class MouseLook : MonoBehaviour
 {
     public float Sensitivity = 2f;
     public float YAxisAngleLock = 90f;
 
     public Transform CameraTransform;
 
     private Transform _playerTransform;
 
     private Vector2 _rotation;
     private Quaternion _playerTargetRot;
     private Quaternion _cameraTargetRot;
 
     private void Start()
     {
         _playerTransform = transform;
         _playerTargetRot = _playerTransform.rotation;
         _cameraTargetRot = CameraTransform.rotation;
     }
 
     private void LateUpdate()
     {
         _rotation.x = Input.GetAxis("Mouse X") * Sensitivity;
         _rotation.y = Input.GetAxis("Mouse Y") * Sensitivity;
 
         _playerTargetRot *= Quaternion.Euler(0f, _rotation.x, 0f);
 
         _cameraTargetRot *= Quaternion.Euler(-_rotation.y, 0f, 0f);
 
         _cameraTargetRot = LockCameraMovement(_cameraTargetRot);
 
         _playerTransform.localRotation = _playerTargetRot;
         CameraTransform.localRotation = _cameraTargetRot;
     }
 
     private Quaternion LockCameraMovement(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, -YAxisAngleLock, YAxisAngleLock);
 
         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 justantorami · Feb 28, 2019 at 07:31 PM 0
Share

@Warmedx$$anonymous$$ints Thanks for the fast answer!

If it's not a problem, could you explain what LookCamera$$anonymous$$ovement() does? I'm having a hard time understanding quaternions.

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

210 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 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 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 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

transform.rotation.z gives different value than expected 1 Answer

How to rotate object in instantiate with float value 1 Answer

Rotation drift with transform.eulerAngles 0 Answers

Object deforms with rotation 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