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 /
avatar image
2
Question by Beks_Omega · Jun 26, 2017 at 04:17 PM · cameraaxiscamera rotaterotatearoundlimit

Limit Y Axis transform.RotateAround

Hello,

I have a camera that I want to rotate around a point (0,0,0) in all directions, but I want to put a clamp on it so that it can't go too far above or below the point. I have seen this question answered for the left and right directions before but never for the vertical one.

I have tried converting the code from these two questions (that basically say the same thing) to work in the vertical direction, but it bugs out at some points along the rotation, and I can't figure out why.

First Question, Second Question

And this is how I tried to convert it:

 //how much we want to rotate by this frame
 float rotX = Input.GetAxis("Mouse X") * rotSpeed;
 float rotY = Input.GetAxis("Mouse Y") * rotSpeed; //(before clamping)
 
 //find current direction
 Vector3 currentDirection = transform.position - Vector3.zero;
 
 //find current angle between basis for clamp & where we are now
 float angle = Vector3.Angle(Vector3.forward, currentDirection);
 
  //finds out if it's up or down
 if (Vector3.Cross(Vector3.forward, currentDirection).x < 0) angle = -angle; 
 
  //find out how much you can move without violating limits
  float newAngle = Mathf.Clamp(angle + rotY, yMinLimit, yMaxLimit);
 
 //grabs how much you are allowed to move the angle from the current angle
 rotY = newAngle - angle;
 
 //spinning the garden
 transform.RotateAround(Vector3.zero, Vector3.up, rotX);
  transform.RotateAround(Vector3.zero, transform.TransformDirection(Vector3.right), -rotY); //vertical rotation

If anyone knows of the correct way to make this work for the Y axis, or a different way to clamp the vertical rotation, I would be super excited to hear it! Ty!

Edit: Seems to be other interest in this question here: http://answers.unity3d.com/questions/627515/clamp-camera-angle-not-working.html

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
3
Best Answer

Answer by Beks_Omega · Jul 06, 2017 at 08:33 PM

A very lovely person a StackOverflow gave me the answer to this question! For anyone finding this in the future here ya go: https://stackoverflow.com/questions/44837086/how-to-limit-clamp-y-axis-rotation-for-transform-rotatearound-unity

And here are the parts of it I used: (because he adds in some stuff about changing distances and stuff)

 void Start()
     {
         //vector from where I am to what I'm rotating around
         initialVector = transform.position - Vector3.zero; 
 
         //current angles of camera (make sure it's looking at what you want
         //to rotate around)
         rotYAxis = transform.eulerAngles.y;
         rotXAxis = transform.eulerAngles.x;
         //distance between me and what I'm rotating around
         distance = Vector3.Magnitude(initialVector);
     }
 
 //You can probably call this in update I just call it from a dif script
 public void Orbit()
     {
         
        //get your inputs
         rotYAxis += Input.GetAxis("Mouse X") * thirdRotSpeed;
         rotXAxis -= Input.GetAxis("Mouse Y") * thirdRotSpeed;
 
        //clamp the angle
         rotXAxis = ClampAngle(rotXAxis, thirdMin, thirdMax);
 
        // convert it to quaternions
         Quaternion toRotation = Quaternion.Euler(rotXAxis, rotYAxis, 0);
         Quaternion rotation = toRotation;
 
        //figure out what your distance should be (so that it's rotating around 
        //not just rotating)
         Vector3 negDistance = new Vector3(0, 0, -distance);
         Vector3 position = rotation * negDistance + Vector3.zero;
 
        //and apply!
         transform.rotation = rotation;
         transform.position = position;
     }
 
 //clamp angle from before
 public static float ClampAngle(float angle, float min, float max)
     {
         if (angle < -360F)
             angle += 360F;
         if (angle > 360F)
             angle -= 360F;
         return Mathf.Clamp(angle, min, max);
     }

Thanks so much again to Fenixrw on StackOverflow for the help!

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 TheGabeMeister · Jul 24, 2019 at 06:33 PM 2
Share

Bro what a life saver! Was trying to rap my head around this but you came to the rescue. It was easier looking at ur code then the stuff you linked. To those wondering how to rotate around a point, I replaced his "Vector3.zero" with "Vector3.Up" in my case so it would effectively do the top of my character, this will likely be another value for you guys but I think that is where you put it.

Thanks!

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

100 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

Related Questions

Clamp Camera Angle not Working 2 Answers

Based on touch rotate camera object 0 Answers

How can I limit my rotation around an object ? 1 Answer

Set max allowed angle? 1 Answer

Clamping position of camera when using RotateAround? 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