Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 toomasb · Jun 08, 2015 at 10:56 AM · camerarotationquaternioneuleranglesclamp

Edited mouselook script rotation clamp not working

I'm using the standard assets MouseLook.cs script (but not the FPSCharacterController, because it doesn't have the features I need). I changed the MouseLook script to fit my needs but I can't seem to get the rotation clamp to work. The clamp flings my camera to the maximum clamp value as soon as it hits the clamp center.

What I'm trying to achieve is a first person camera, that can't turn the character (which is turned with the keyboard) and is clamped to, well, a more or less logical field of view when the character turns its head.

So the code at the moment is:

 using System;
 using UnityEngine;
 
 public class MouseLook : MonoBehaviour
 {
     public GameObject player;
     public float XSensitivity = 2f;
     public float YSensitivity = 2f;
     public float xMin = -90F;
     public float xMax = 90F;
     public float yMin = -90F;
     public float yMax = 90F;
     public bool smooth;
     public float smoothTime = 5f;
 
     private Quaternion m_CharacterTargetRot;
     private Quaternion m_CameraTargetRot;
 
     public void Start()
     {
         m_CharacterTargetRot = player.transform.localRotation;
         m_CameraTargetRot = GetComponent<Camera>().transform.localRotation;
     }
 
     public void LookRotation(Transform player, Transform camera)
     {
         float yRot = Input.GetAxis("Mouse X") * XSensitivity;
         float xRot = Input.GetAxis("Mouse Y") * YSensitivity;
         m_CameraTargetRot *= Quaternion.Euler (-xRot, yRot, 0f);
         
         m_CameraTargetRot = ClampRotationAroundXAxis (m_CharacterTargetRot);
         transform.localRotation = Quaternion.Slerp (transform.localRotation, m_CameraTargetRot,
             smoothTime * Time.deltaTime);
     }
 
     Quaternion ClampRotationAroundXAxis(Quaternion q)
     {
         float yDeg = m_CameraTargetRot.eulerAngles.y;
         float xDeg = m_CameraTargetRot.eulerAngles.x;
         float z = 0f;
         float y = Mathf.Clamp (yDeg, yMin, yMax);
         float x = Mathf.Clamp (xDeg, xMin, xMax);
         q = Quaternion.Euler (x, y, z);
         return q;
     }
 }

LookRotation, like originally, is called from the character controller script (that bit works)

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Bizzare problem with limiting camera veritcal rotation 2 Answers

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

How to make camera rotation relative to target. 3 Answers

Quaternion - Euler Angles, weird variable swapping when converting 2 Answers

Clamping a wrapping rotation. 6 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