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 /
  • Help Room /
avatar image
0
Question by kasquer · Oct 17, 2016 at 11:53 AM · rotationjavascriptmouselookrotatearoundclamp

How to clamp transform.RotateAround using Javascript?

Hello, I ran into a problem while making camera script and am unsure on how to fix it myself. I am using transform.RotateAround to make the camera look up and down based on mouse movement but I don't want the camera to be able to do a full circle. How would I go about clamping this rotation?

Rotation I want to clamp:

 transform.RotateAround (Ship.localPosition, transform.right, -Input.GetAxis("Mouse Y") * Time.deltaTime * 200);    
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 JScotty · Oct 19, 2016 at 07:40 AM

I did some research and found this: http://answers.unity3d.com/questions/438836/limit-camera-rotation-with-rotatearound.html

Sot his would be the best code for you

 //[SerializeField] show private variable in editor
     [SerializeField] private float maxRotX = 10; // max rotation angle
     [SerializeField] private float minRotX = -10; // min rotation angle
 
     [SerializeField] private Transform target; // target (ex: ship)
 
     private Vector3 initialVector = Vector3.forward;
 
     void Start () {
         initialVector = transform.position - target.position;
         initialVector.y = 0;
     }
     
     void Update () {
         float rotateDegrees = 0f;
         float mouseYDelta = -Input.GetAxis("Mouse Y");
         float speed = Time.deltaTime * 200;
 
         if (mouseYDelta > 0) { // up
             rotateDegrees += speed;
         } else if (mouseYDelta < 0) { // down
             rotateDegrees -= speed;
         }
              
         Vector3 currentVector = transform.position - target.position;
         currentVector.x = 0;
         float angleBetween = Vector3.Angle(initialVector, currentVector) * (Vector3.Cross(initialVector, currentVector).x > 0 ? 1 : -1); // calculating angle    
         float newAngle = Mathf.Clamp(angleBetween + rotateDegrees, minRotX, maxRotX); // clamping angle
         rotateDegrees = newAngle - angleBetween;
          
         transform.RotateAround(target.position, Vector3.right, rotateDegrees); // rotation
     }


Hope this works out for you.

Comment
Add comment · Show 1 · 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 kasquer · Oct 19, 2016 at 05:31 PM 0
Share

With this my camera seems to be freaking out and rotating endlessly. I checked for errors and didn't find any. Are any other things in my script breaking it? $$anonymous$$y current camera script:

 #pragma strict
 var Ship : Transform;
 var CR : Transform;
 var Distance : int;
 var $$anonymous$$axZoomOut : float;
 var $$anonymous$$axZoomIn : float;
 
 //---------------------
 @SerializeField
 private var maxRotX : float = 10;
 @SerializeField
 private var $$anonymous$$RotX : float = -10;
 @SerializeField
 private var target : Transform;
 @SerializeField
 private var initialVector : Vector3 = Vector3.right;
 //---------------------
 
 function Update () {
 //---------------------
     var rotateDegrees : float = 1f;
     var mouseYDelta : float = Input.GetAxis("$$anonymous$$ouse Y");
     var speed : float = Time.deltaTime * 1;
     
     if (mouseYDelta > 0) {
         rotateDegrees += speed;
     } else if (mouseYDelta) {
         rotateDegrees -= speed;
     }
     
     var currentVector : Vector3 = transform.position - target.position;
     currentVector.x = 0;
     
     var angleBetween : float = Vector3.Angle(initialVector, currentVector) * (Vector3.Cross(initialVector, currentVector).x > 0 ? 1 : -1);
     var newAngle : float = $$anonymous$$athf.Clamp(angleBetween + rotateDegrees, $$anonymous$$RotX, maxRotX);
     
     rotateDegrees = newAngle - angleBetween;
     
     transform.RotateAround(target.position, transform.right, rotateDegrees);
 //---------------------
     
     //Check Distance From Ship
     Distance = Vector3.Distance(CR.position, Ship.position);
     
     //Zoom In
     if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") > 0 && Distance >= $$anonymous$$axZoomIn + 1) {
         CR.position += CR.forward*Time.deltaTime*20;
     }
     //Zoom Out
     if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") < 0 && Distance <= $$anonymous$$axZoomOut - 1) { // back
         CR.position -= CR.forward*Time.deltaTime*20;
     }
     
     //Fix Zoom Out
     if (Distance >= ($$anonymous$$axZoomOut+1)) {
         CR.position += CR.forward;
     }
     //Fix Zoom In
     if (Distance <= ($$anonymous$$axZoomIn-1)) {
         CR.position -= CR.forward;
     }
     
     CR.RotateAround (Ship.localPosition, CR.up, Input.GetAxis("$$anonymous$$ouse X") * Time.deltaTime * 200);    
     
     if(!Input.Get$$anonymous$$ouseButton(2)) {
         //transform.RotateAround (CR.localPosition, transform.right, -Input.GetAxis("$$anonymous$$ouse Y") * Time.deltaTime * 200);    
     } else if(Input.Get$$anonymous$$ouseButton(2)) {
         transform.localRotation.x = 0;
         transform.localRotation.y = 0;
         transform.localRotation.z = 0;    
     }
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotating around another object without parent/child relation?? 0 Answers

Is this a good way to Clamp a Rotation? 2 Answers

How can I find the angle an object needs to turn towards a target from a rotated perspective? 0 Answers

Rotate around a point with a specific angle 0 Answers

how do i Limit camera rotation on Y-Axis 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