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
0
Question by Trevdevs · Oct 17, 2016 at 11:50 PM · c#clamplocalrotationclamped rotation

Clamping objects localrotation not working (C#)

I don't know why i'm having such a hard time with this it's a basic script. Basically I have a camera that i need to be able to orbit another object but only to an extent on the x axis it can circle it on y as much as it wants for all i care. But the x needs to stay between two values which I assigned via floats and tried restricting via Mathf.Clamp however the Vector3 does stay within the boundaries of the clamp when i apply it to the rotation it's as if it doesn't apply.

This is a child of an object i'm working with so I must use localRotation instead of rotation

Here is the script and to be more precise it's line 26 where it should update the objects localrotation but is not.

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour 
 {
     public Transform target;
     public Camera camera;
 
     public float minX;
     public float maxX;
 
     void Update()
     {
         //Allow Horizontal and Vertical Rotation to be controlled by the mouse
         camera.transform.RotateAround (target.position, target.up, Input.GetAxis ("mouse_axis_0"));
         camera.transform.RotateAround (target.position, target.right, Input.GetAxis ("mouse_axis_1"));
 
         //Store the current rotation in euler angles
         Vector3 localAngles = camera.transform.localEulerAngles;
 
         //Make sure the the player cannot move the mouse to far horizotnally and vertically
         localAngles.x =  Mathf.Clamp (camera.transform.localRotation.eulerAngles.x, minX, maxX);
         localAngles.z = 0f;
 
         //Apply the rotation
         camera.transform.localRotation = Quaternion.Euler (localAngles);
 
         //Make sure the camera is looking at crash
         camera.transform.LookAt (target);
     }
 }
 
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

2 Replies

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

Answer by b1gry4n · Oct 18, 2016 at 01:37 AM

the very last line of your update function tells the camera to lookat the target, everything youre doing before this is pretty much ignored because of that, you probably want to "lookat" before you do min/max checks.

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 Trevdevs · Oct 18, 2016 at 04:48 AM 0
Share

True now that I think about it that would make sense.

avatar image Trevdevs · Oct 18, 2016 at 04:56 AM 0
Share

Holy cow... this was actually the problem... I would have never expected this due to the camera going above the player while rotating around it, it was doing exactly as you explained bypassing the constraints.

avatar image
0

Answer by YesNoKonrad · Oct 18, 2016 at 01:04 AM

My answer consists of 2 parts:

1) The script should work fine even though odd choices are made, I assume you did input degrees for the min and max values even though unity uses radians.

2) You may consider to cleanly calculate the new position, then clamp it, then transform it. That way you could easily extract code parts and reuse or replace them. It makes it also easier to read when you come back later. And believe me, you will come back later. And - assuming this script grows even a little bit - you'll be like: wtf is going on here.

Hope 1) helps :)

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 Bunny83 · Oct 18, 2016 at 01:50 AM 1
Share

Unity doesn't use radians in most of it's API. Only their $$anonymous$$athf implementations use radians (since it mainly just wraps System.$$anonymous$$ath). All other methods of the actual API uses degrees. EulerAngles, RotateAround, Rotate, Vector3.Angle, ... all use degrees.

The actual problem is that Unity doesn't store the rotation as euler angles but as quaternion. Euler angles is an "over-specified" system so you can always have two different ways to express the exact same rotation. When Unity converts the rotation from it's quaternion representation into eulerangles you don't know which eulerangles representation you end up with. When you rotate beyond a certain point the conversion might suddenly return a different representation.

You should never do a read-modify-write operation on eulerangles as the eulerangles is not a reliable and continuous representation of the rotation.

So yes, the best way if you need $$anonymous$$ and max angles is to use your own variable to hold the current rotation and just apply it to the camera. That way you can ensure you always use the same euler angles representation.

$$anonymous$$eep in $$anonymous$$d that eulerangles suffer from gimbal lock while Quaternions do not.

avatar image Trevdevs · Oct 18, 2016 at 04:52 AM 0
Share

I inputted a float value in the inspector x$$anonymous$$in = 0, x$$anonymous$$ax = 50. This was actually a cleaned up script from and old programmer trust me it looks a lot better than it did before. But I do think I just have the order a bit confused I shall try that.

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

228 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 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 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

Clamped rotation of turret problem 0 Answers

Problem with Mathf.Clamp and Mouse Follow Script 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why is my object's local x rotation not going past 90 degrees? 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