Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by Sixtoo · Apr 30, 2018 at 03:31 PM · camerarotationeulerangles

Clamping Between Two Different Ranges

Hello, I commented the code below to make it easier. I am trying to make a free rotating camera. I've got most of it, but now I want to limit it so the user can't flip the camera upside down. I am using RTSControl.transform.eulerAngles = rotation to achieve this. The problem is that if I'm using this method I would need to clamp between either 315 and 0 ... OR ... between 0 and 45. How can I clamp between two ranges like this, or can someone else explain a good way of achieving this? Thank you very much

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEngine.EventSystems;
     using UnityEngine.UI;
     
     public class CameraController : MonoBehaviour
     {
         GameObject RTSControl;
     
         public Camera rtsCamera;
     
         private void Start()
         {
             RTSControl = GameObject.Find("RTSControl");
     
             rtsCamera = GameObject.Find("rtsCamera").GetComponent<Camera>();
         }
     
         void Update()
         {
             //Lets me rotate all the way in all directions
             //However also lets me rotate until the camera is upside down!
             if (Input.GetMouseButton(1))
             {
                 float yaw = Input.GetAxis("Mouse X");
                 float pitch = Input.GetAxis("Mouse Y");
     
                 RTSControl.transform.RotateAround(RTSControl.transform.position, Vector3.up, yaw * 500f * Time.deltaTime);
                 RTSControl.transform.Rotate(-pitch * 500f * Time.deltaTime, 0, 0);
             }
             
             //Clamps the rotation between 0 and 90
             //However I need it between 315 and 0 ... OR between 0 and 45
             Vector3 rotation = RTSControl.transform.eulerAngles;
             rotation.x = Mathf.Clamp(rotation.x, 0, 90);
             RTSControl.transform.eulerAngles = rotation;
         }
     }



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 Sixtoo · May 01, 2018 at 08:32 PM

I'm still looking for a better way to clamp between two different ranges... but I've come up with a very poor solution to the problem in the mean time. Basically I store the last known angle that is in range, and if the camera rotation goes out of range then it snaps back to the last known "good" angle. Unfortunately this causes the camera to stutter when going out of the range.

     Vector3 lastGoodAngle = new Vector3();

     void Update()
     {
         Vector3 rot = RTSControl.transform.eulerAngles;
 
         //Need to find a better way to limit rot.x angle
         if (Input.GetMouseButton(1) && (rot.x < 45 || rot.x > 320))
         {
             float yaw = Input.GetAxis("Mouse X");
             float pitch = Input.GetAxis("Mouse Y");
 
             RTSControl.transform.RotateAround(RTSControl.transform.position, Vector3.up, yaw * 500f * Time.deltaTime);
             RTSControl.transform.Rotate(-pitch * 500f * Time.deltaTime, 0, 0);
 
             lastGoodAngle = rot;
         }
         else
         {
             RTSControl.transform.eulerAngles = lastGoodAngle;
         }
 }

I'm using this because for some reason when I tried to Mathf.Clamp the angle and set the transform.eulerAngles the camera goes skew. I think it may have been because I was trying to only set the rot.x value without recalculating the whole transform.eulerAngles ? I don't know.


Still looking for a solution to clamp the value between one of the two different ranges

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

209 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

Related Questions

Camera X Rotation Problems 0 Answers

Limit camera rotaion on x axis euler angles 1 Answer

UI compass pointing to a 3D object 0 Answers

transform.eulerAngles problem with parent rotation 0 Answers

Limit Cam rotation 0 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