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 /
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 $$anonymous$$ · 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: 

Follow this Question

Answers Answers and Comments

105 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

Related Questions

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

Limit Rotation of Camera around a player using keyboard 1 Answer

Simple Rotation 1 Answer

i have rotated an gameobject but the axis of the object did not change 2 Answers

make projectiles always hit target 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