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 Zarack · Jan 29, 2013 at 12:49 AM · rotationeuleranglesrotatearoundclamp

Rotation not observing limits

I've been trying to get this camera controller to work all day after moving away from a mouse orbit script. Currently, four planes control the movement of the camera around an object. Left and right work with no qualms as there are no limits as to their range. I want the local rotation of the camera to go no further than 80 degrees or less than 0 or 5 (whatever works at this point). The conditions limit the rotation when going up (stops at 80.093141ish) but it completely ignores the bottom limit. Blasts right through 0 and heads straight into the 350 range.

Here is my code:

 var myCamera : Camera;
 var hit : RaycastHit;
 var ray : Ray;
 
 var target : Transform;
 var distance = 10.0;
 
 var xSpeed = 250.0;
 var ySpeed = 120.0;
 
 var yMinLimit = 5;
 var yMaxLimit = 80;
 
 private var x = 0.0;
 private var y = 0.0;
 
 function Start () {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
     
     var rotation = Quaternion.Euler(y, x, 0);
     var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
         
     myCamera.transform.rotation = rotation;
     myCamera.transform.position = position;
 
     // Make the rigid body not change rotation
        if (rigidbody)
         rigidbody.freezeRotation = true;
 }
  
 function Update () {
 
     var current = myCamera.transform.rotation.eulerAngles;
 
     if(Input.touchCount == 1) {
     
         var touch: Touch = Input.touches[0];
         ray = myCamera.ScreenPointToRay(Input.touches[0].position);
         
         if(Physics.Raycast(ray.origin, ray.direction,hit)){
  
             switch(hit.collider.name){
             case "cameraRight":
                 Debug.Log("cameraRight tapped");
                 myCamera.transform.RotateAround(target.position, Vector3.up, -30 * Time.deltaTime);
              break;
              case "cameraLeft":
                 Debug.Log("cameraLeft tapped");
                 myCamera.transform.RotateAround(target.position, Vector3.up, 30 * Time.deltaTime);
              break;
              case "cameraUp":
                  Debug.Log("cameraUp tapped");
                  if(current.x < yMaxLimit) {
                      myCamera.transform.RotateAround(target.position, target.right, 15 * Time.deltaTime);
                  }
              break;
              case "cameraDown":
                  Debug.Log("cameraDown tapped");
                  if(current.x > yMinLimit) {
                      myCamera.transform.RotateAround(target.position, target.right, -15 * Time.deltaTime);
                  }
              break;
          }
         }
         else if (touch.phase == TouchPhase.Ended && !Physics.Raycast(ray.origin, ray.direction,hit)) {
             Debug.Log("tap cancelled");
         }
     }
 }

What am I doing wrong?

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 Zarack · Jan 29, 2013 at 01:04 AM 0
Share

transform.Rotate(Vector3.up, 30 * Time.deltaTime);

Should also be in there. Deleted it by mistake. This line rotates an empty GameObject that acts as the target. The up and down rotations happen over the target's X-axis (target.right).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zarack · Jan 29, 2013 at 01:49 AM

So... I solved it I guess? I'm not sure if this is optimal or efficient, but it works.

Here is my solution:

 var yMinLimit = 5.0;
 var yMaxLimit = 80.0;
 
 private var lowerLimitHit = false;
 private var upperLimitHit = false;
 
 function Update () {
 
     var current = myCamera.transform.rotation.eulerAngles;
     
     //Has the lowerLimit or upperLimit been hit?
     lowerLimitHit = current.x < yMinLimit;
     upperLimitHit = current.x > yMaxLimit;
     
     //AFTER THE BREAK....
     
     case "cameraUp":
         Debug.Log("cameraUp tapped");
         if(!upperLimitHit) {
             myCamera.transform.RotateAround(target.position, target.right, 15 * Time.deltaTime);
         }
     break;
     case "cameraDown":
         Debug.Log("cameraDown tapped");
         if(!lowerLimitHit) {
             myCamera.transform.RotateAround(target.position, target.right, -15 * Time.deltaTime);
         }
     break;
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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

trying to RotateAround an object around another object using mouse 1 Answer

Edited mouselook script rotation clamp not working 0 Answers

Limit Rotation of physics object in 2D 1 Answer

Clamp Rotation Problem 1 Answer

Constrained rotation problem 2 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