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 OldManTies · Oct 25, 2016 at 09:24 PM · c#rotationfpsquaternionclamp

Clamp Quaternions Rotation for camera

I'm pretty new to the whole 'Quaternion' bussines and I'm trying to keep the camera from rotating all the way up or down. So I figured 'Mathf.Clamp' would be my best shot, but I can't figure this one out.

 public float rotateVel = 100;
     public float inputDelay = 0.1f;
 
     public float yMin;
     public float yMax;
     private float yRot;
 
     public float turnInput;
 
     private Quaternion targetRotation;
 
     // Use this for initialization
     void Start () {
         targetRotation = transform.rotation;
         turnInput = 0;
     }
     
     // Update is called once per frame
     void Update () {
         turnInput = Input.GetAxis("Mouse Y");

         if (Mathf.Abs(turnInput) > inputDelay)
         {
             targetRotation *= Quaternion.AngleAxis(rotateVel * -turnInput * Time.deltaTime, Vector3.right);
         }
         transform.rotation = targetRotation;
     }

I was hoping somebody could help me out here.

Thanks in advance :D

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
0

Answer by Bioinformatizer · Oct 25, 2016 at 09:52 PM

This was a problem for me also. I have a fix, but the coding elegance is lacking.

 // CameraOrbit - Bioinf
 using UnityEngine;
 using System.Collections;
 
 public class CameraOrbit : MonoBehaviour
 {
     public float minAngle =  -80;
     public float maxAngle = 80;
     public float minDistance = 3;
     public float MaxDistance = 10;
     public float currentDistance = 5;
     public float zoomSensitivity = 2;
     [Space]
     public GameObject currentTarget; 
 
     private bool rightDown;
     private float mouseScroll;
     private float lastDistance;
     private Vector2 rightClickMouseDelta = Vector2.zero;
     private Camera cam;
 
     void Start()
     {
         cam = gameObject.GetComponent<Camera>();
         lastDistance = currentDistance;
         if (currentTarget == null)
         {
             CameraFocus();
         }
     }
     void CameraFocus()
     {
         // decides the camera target and ortho and zoom level
     }
     void Update() {
 
         GetInput();
 
         if (rightDown) // && spinForce > currentSpeed
         {
             //StartCoroutine(ChangeOrbit());
             ChangeOrbit();
         }
         if (mouseScroll != 0)
         {
             lastDistance = currentDistance;
             currentDistance += (-mouseScroll * Time.deltaTime * zoomSensitivity);
             ZoomCamera();
         }
     }
     void GetInput()
     {
         if (Input.GetMouseButton(1) && rightDown)
         {
             rightClickMouseDelta += new Vector2(Input.GetAxis("Mouse X"), -Input.GetAxis("Mouse Y"));
         }
         else if (Input.GetMouseButton(1) && !rightDown)
         {
             rightDown = true;
             rightClickMouseDelta = Vector2.zero;
         }
         else
         {
             rightDown = false;
         }
         mouseScroll = Input.GetAxis("Mouse ScrollWheel");
     }
     void ZoomCamera()
     {
         gameObject.transform.position *= (currentDistance - lastDistance + 1.0f);
         gameObject.transform.position = Vector3.ClampMagnitude(gameObject.transform.position, MaxDistance*3.14f);
     }
     //IEnumerator ChangeOrbit()
     void ChangeOrbit()
     {
         Vector3 angles = transform.eulerAngles;
         angles.z = 0;
         transform.eulerAngles = angles;
         if (currentTarget)
         {
             transform.RotateAround(currentTarget.transform.position, Vector3.up, rightClickMouseDelta.x);
             // Lock Rotation Function
             float deltaAngle = rightClickMouseDelta.y * 0.25f;
             if ((deltaAngle + angles.x) > maxAngle && (deltaAngle + angles.x) < 180 || (deltaAngle + angles.x) < minAngle + 360 && (deltaAngle + angles.x) > 180)
             {
                 deltaAngle = 0;
             }
             transform.RotateAround( currentTarget.transform.position,
                                     new Vector3(Mathf.Sin(Mathf.Deg2Rad * (angles.y + 90)), 0f, Mathf.Cos(Mathf.Deg2Rad * (angles.y + 90))),
                                     deltaAngle);
             gameObject.transform.LookAt(currentTarget.transform);
         }
         else {
             transform.RotateAround(Vector3.zero, Vector3.up, rightClickMouseDelta.x);
             // Lock Rotation Function
             float deltaAngle = rightClickMouseDelta.y * 0.25f;
             if ((deltaAngle + angles.x) > maxAngle && (deltaAngle + angles.x) < 180 || (deltaAngle + angles.x) < minAngle + 360 && (deltaAngle + angles.x) > 180)
             {
                 deltaAngle = 0;
             }
             transform.RotateAround( Vector3.zero,
                                     new Vector3(Mathf.Sin(Mathf.Deg2Rad * (angles.y + 90)), 0f, Mathf.Cos(Mathf.Deg2Rad * (angles.y + 90))),
                                     deltaAngle);
             gameObject.transform.LookAt(Vector3.zero);
         }
     }
 }
 

Ignore CameraFocus here and set your own gameObject as target if you want.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

how to clamp y axis with Quaternion.Euler in unity 1 Answer

Trouble with Camera Rotation Math 2 Answers

Weird shake when I try to rotate player according to Virtual Cameras rotation. 0 Answers

Transform rotation and position on key input: rotation only working the first time 2 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