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
2
Question by Danz0r77 · Apr 15, 2013 at 08:41 PM · camerarotationrotatearound

Limit camera rotation with RotateAround

Hi

I'm struggling to find the right way to go about this. I think I'm down the wrong alley. What I want is for my camera to rotate left and right (while left and right arrow keys are down).

This works OK:

 if (Input.GetKey(KeyCode.LeftArrow)) {
 
         transform.RotateAround(centerPoint.position,Vector3(0,-1,0),20 * Time.deltaTime);
 
     }

I want to limit the max angles that can be reached though. 30 degrees either way. I'm not sure what the best way to go about that is. From what I've read I think RotateAround maybe isn't the way to do it, I need the effect that function gives though - I.E translating the movement at the same time to keep the focus in the center.

So, what would be the best way of achieving this?

Thanks

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by whebert · Apr 15, 2013 at 09:31 PM

The following would limit your orbit angle around a target relative to whatever your initial position with respect to that target is - not necessarily the target's forward or other direction.

 using UnityEngine;
 using System.Collections;
 
 public class OrbitCamera : MonoBehaviour {
     
     public float rotateSpeed = 20.0f;
     public float angleMax = 30.0f;
     public Transform target;
     
     private Vector3 initialVector = Vector3.forward;
     
     // Use this for initialization
     void Start () {
         
         if(target != null)
         {
             initialVector = transform.position - target.position;
             initialVector.y = 0;
         }
     
     }
     
     // Update is called once per frame
     void Update () {
         
         if(target != null)
         {
             float rotateDegrees = 0f;
             
             if (Input.GetKey(KeyCode.LeftArrow)) 
             {
                 rotateDegrees += rotateSpeed * Time.deltaTime;
             }
             if (Input.GetKey(KeyCode.RightArrow)) 
             {
                 rotateDegrees -= rotateSpeed * Time.deltaTime;
             }
             
             Vector3 currentVector = transform.position - target.position;
             currentVector.y = 0;
             float angleBetween = Vector3.Angle(initialVector, currentVector) * (Vector3.Cross(initialVector, currentVector).y > 0 ? 1 : -1);            
             float newAngle = Mathf.Clamp(angleBetween + rotateDegrees, -angleMax, angleMax);
             rotateDegrees = newAngle - angleBetween;
         
             transform.RotateAround(target.position, Vector3.up, rotateDegrees);
         }
     
     }
 }
Comment
Add comment · Show 4 · 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 Danz0r77 · Apr 16, 2013 at 07:59 AM 0
Share

Thanks, this is great

avatar image abreu20011 · Sep 23, 2013 at 02:55 AM 0
Share

Thanks very much!!! helped me very much ^^

avatar image Tragon · Oct 07, 2013 at 07:11 AM 0
Share

The code worked great but I had little problem understanding the code, so could you please tell me what should I do to make it rotate in any other axis than that it does with this code.

avatar image benqowny · Mar 04, 2020 at 03:54 PM 0
Share

Hey guys,

I did test the code above it works almost perfect. But still got some issue like this

alt text

It starts shaking when its near to the limits.

avatar image
0

Answer by Yokimato · Apr 15, 2013 at 08:56 PM

  1. Figure out the axis you're restricting rotation on.

  2. Clamp the rotation on that (-30, 30).

An example:

 // after rotation
 Vector3 objRotation = transform.rotation.eulerAngles;
 float clampedY = Mathf.Clamp(objRotation.y, -30f, 30f);
 transform.rotation.eulerAngles = new Vector3(objRotation.x, clampedY, objRotation.z);
 
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
avatar image
0

Answer by benqowny · Mar 04, 2020 at 02:38 PM

Hey guys!

I did the rotateAround method like you showed above, but can't fix this shaking when near the angle limits.

alt text


rotatearoundneedfix.gif (385.5 kB)
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 benqowny · Mar 04, 2020 at 03:59 PM 0
Share

Hey again! I fixed the issue.

It turn out that

float angleBetween = Vector3.Angle(initialVector, currentVector) * (Vector3.Cross(initialVector, currentVector).y > 0 ? 1 : -1);

is not equal to

Vector3.SignedAngle(toSwordVec.normalized, forwardVec.normalized, Vector3.up);

wich I used.

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

16 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

Related Questions

TPS Rotation Camera (similar to WoW or any other mmo) 0 Answers

An alternative to RotateAround for collisions 0 Answers

How to limit the angle of a camera with transform.RotateAround? 1 Answer

Player making a 360° looping, camera not following 0 Answers

easing camera rotation after mouse up? 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