• 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 /
This question was closed Apr 01, 2018 at 02:33 PM by hexagonius for the following reason:

QAs about clamping camera rotations have been asked and answered many times before

avatar image
0
Question by Davidblackberry1 · Apr 01, 2018 at 02:18 PM · rotationquaternionmathf.clampfirst-person

I'm having trouble clamping the rotation of the camera?

Hello, I seem to be having some trouble integrating a function that I took from the MouseLook script that comes with importing the Characters package. Currently, it does not seem to be doing anything, but I would like it to limit the x of the camera rotation to only be -90 to 90. I'll put slashes after the important lines, the rest is working fine.

 using UnityEngine;
 using System;
 using System.Collections;
 
 public class CameraMouseLook : DavidBehaviour{
 
     Vector2 MouseLook;
     Vector2 SmoothV;
     public float Sensitivity = 5;
     public float Smoothing = 2;
 
     public bool CanMove;
 
     void Update(){
         Camera.main.transform.localRotation = ClampRotationAroundXAxis (Camera.main.transform.localRotation);//
 
         if (CanMove) {
             var MD = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
 
             MD = Vector2.Scale (MD, new Vector2 (Sensitivity * Smoothing, Sensitivity * Smoothing));
             SmoothV.x = Mathf.Lerp (SmoothV.x, MD.x, Smoothing);
             SmoothV.y = Mathf.Lerp (SmoothV.y, MD.y, Smoothing);
             MouseLook += SmoothV;
 
             Camera.main.transform.localRotation = Quaternion.AngleAxis (-MouseLook.y, Vector3.right);
             transform.localRotation = Quaternion.AngleAxis (MouseLook.x, transform.up);
         }
     }
     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, -90, 90);//
 
         q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);//
 
         return q;//
     }//
 }



Comment
Add comment · Show 1
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 meat5000 ♦ · Apr 01, 2018 at 02:36 PM 0
Share

You seem to be mixing Euler and Quaternions. This will never work.

0 Replies

  • Sort: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

106 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

Related Questions

How to make a gameobject have a rotation constraint? 1 Answer

Constrain object rotation set by mouse? 1 Answer

Need help on making a semi-circle rotation 1 Answer

How to make frame rate independent rotations? 1 Answer

Quaternion LookRotation issue 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges