Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 awplays49 · Dec 25, 2014 at 09:08 PM · rotationclampmathf.clampinvert

clamp rotation the other way?

what if you wanted to clamp a rotation to be between 90 and 270, so that you could look up and down, but it flips the other way! this would happen when the quaternion value "0" is between the min clamp and max clamp. in this case, what would you do?

Comment
Add comment · Show 2
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 awplays49 · Dec 25, 2014 at 09:09 PM 0
Share

more specifically, i have this:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : $$anonymous$$onoBehaviour {
     
     public int Sensitivity;
     
     private float $$anonymous$$ouseY;
     private float $$anonymous$$ouseYSet;
     private float CameraAngle;
 
     public GameObject CameraTarget;
     
     void Start () {
         
     }
     
     void Update () {
         // Rotation controls
         $$anonymous$$ouseY += Input.GetAxisRaw ("$$anonymous$$ouse Y");
         transform.localRotation = Quaternion.Euler ($$anonymous$$athf.Clamp (transform.localRotation.x, 90, -90), transform.localRotation.y, transform.localRotation.z);
         transform.localPosition = new Vector3 (transform.localPosition.x, $$anonymous$$athf.Clamp (transform.localPosition.y, CameraTarget.transform.localPosition.y, CameraTarget.transform.localPosition.y + 50), $$anonymous$$athf.Clamp (transform.localPosition.z, CameraTarget.transform.localPosition.z + 25, CameraTarget.transform.localPosition.z + 50));
         if ($$anonymous$$ouseYSet != $$anonymous$$ouseY)
         {
             transform.localRotation = Quaternion.Euler (transform.localRotation.x - ($$anonymous$$ouseY - $$anonymous$$ouseYSet) * Sensitivity * 10, transform.localRotation.y, transform.localRotation.z);
             transform.localPosition = new Vector3 (transform.localPosition.x, transform.localPosition.y - ($$anonymous$$ouseY - $$anonymous$$ouseYSet) * Sensitivity * 10, transform.localPosition.z + ($$anonymous$$ouseY - $$anonymous$$ouseYSet) * Sensitivity * 10);
             $$anonymous$$ouseYSet = $$anonymous$$ouseY;
         }
     }
 }

avatar image awplays49 · Dec 25, 2014 at 09:10 PM 0
Share

also the color of the scene background flashes sometimes when rotating up and down

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Dec 25, 2014 at 10:19 PM

This is a recurrent error: localPosition is a Quaternion, a weird creature, and not those nice angles we see in the Inspector's Rotation field - they're actually localEulerAngles.

The best way to limit rotation is to save the initial localEulerAngles in a Vector3 variable and "rotate" the desired angle mathematically:

  using UnityEngine;
  using System.Collections;
  
  public class CameraController : MonoBehaviour {
      
      public int Sensitivity;
      
      private float MouseY;
      private Vector3 CameraAngle;
  
      public GameObject CameraTarget;
      
      void Start () {
          CameraAngle = transform.localEulerAngles;
      }
      
      void Update () {
          // Rotation controls
          CameraAngle.x += Input.GetAxis("Mouse Y") * Sensitivity * Time.deltaTime;
          CameraAngle.x = Mathf.Clamp(CameraAngle.x, -90, 90);
          transform.localEulerAngles = CameraAngle;
      }

This code takes care of the rotation control, but doesn't affect position.

Comment
Add comment · Show 2 · 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
avatar image awplays49 · Dec 25, 2014 at 10:21 PM 0
Share

camera angle is type float though! :)

avatar image aldonaletto · Dec 25, 2014 at 11:09 PM 0
Share

In this case the camera Euler angles must be saved into a Vector3 variable. I changed the CameraAngle declaration to Vector3 and used it for that purpose. You can get the current angle in CameraAngle.x.

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

27 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

Related Questions

Mathf.Clamp() jumping back to max value after reaching min value. 1 Answer

How to use Mathf.Clamp with child objects? 0 Answers

How to clamp player to viewport constraints when rotating 1 Answer

Align GameObject to Terrain angle 2 Answers

Clamping a character's head rotation. 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