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 DrakeXrd · May 04, 2018 at 04:27 AM · transform.rotationtransform.rotate

Stopping transform.Rotate on an exact rotation value

I'm trying to write a script where if the player pushes the "A" key, the camera will rotate -90 degrees on the y-axis, thus moving the camera's y-rotation from 0 to 270. The camera is meant to continue rotating even after the effects of Input.GetKeyDown have taken place, and should only stop when the y-rotation of the camera is 270 degrees. However, whenever I try to get the camera to stop rotating exactly when it's at 270 degrees (ex. "if (transform.localEulerAngles.y == 270)"), the camera will almost always keep on rotating. The best fix I could come up with is changing "==" to "<=", so that the transform.rotation function will stop if the rotation of the camera is less than or equal to 270, but this solution almost never stops right at 270 either. It'll stop the transform.Rotate function a bit later (usually between 268 degrees and 269 degrees, depending on the rotation speed), so I set up another line of code that would set the rotation of the camera to 270 as soon as the transform.Rotate function stops. However, this leads to an annoying camera jitter that occurs when the rotation value of the camera suddenly changes between whatever rotation value the transform.Rotate function stopped on and 270 degrees, and if the player keeps pressing the "A" key the jitter will keep on occurring. Is there any way to have the transform.Rotate function end on an exact rotation value, or at least a way to remove the "jitter"? Here is my code:

 public GameObject cam;
     public float rotateSpeed;
     public bool isRotating;
     public Transform targetRotation270;
 
     // Use this for initialization
     void Start () {
         isRotating = false;
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         if (Input.GetKeyDown(KeyCode.A))
         {
             isRotating = true;
             RotateCam(270);
         }
         else if (!Input.GetKeyDown(KeyCode.A) && isRotating)
         {
             RotateCam(270);
         }
     }
 
     void RotateCam (float targetAngle)
     {
         if (cam.transform.localEulerAngles.y >= targetAngle || cam.transform.localEulerAngles.y == 0)
         {
             cam.transform.Rotate(0, -rotateSpeed * Time.deltaTime, 0);
         }
         else
         {
             isRotating = false;
             cam.transform.Rotate(0, 0, 0);
             cam.transform.rotation = targetRotation270.rotation;
         }
     }
 }

Note: GameObject cam is the camera I'm trying to rotate, and Transform targetRotation270 has the rotation values (0, 270, 0).

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
1

Answer by anthot4 · May 04, 2018 at 07:51 AM

Try using mathf.clamp:

Vector 3 LimitRot = cam.transform.localeulerangles;

LimitRot.y = Mathf.Clamp(LimitRot,0,270);

cam.transform.localeulerangles = LimitRot;

That should work but note it is untested!

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 DrakeXrd · May 04, 2018 at 04:28 PM 0
Share

Thank you for your help! However, now the camera rotates immediately to 270, no matter what rotateSpeed is set. Additionally, the same problem of transform.Rotate not stop precisely persists. I placed the code you provided in FixedUpdate() before the Input.Get$$anonymous$$eyDown functions. Any suggestions?

avatar image anthot4 DrakeXrd · May 08, 2018 at 08:08 AM 0
Share

Sorry, I should have made it clearer where to put it, put the code in the rotatecam function after you have rotated your camera on line 28. I would remove the if statement you have in that function and after my code put something like this:

if (LimitRot == 270){ // if we have reached 270 degrees

isRotating = false;

}

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

82 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

Related Questions

Transform.RotateAround Issues 1 Answer

transform.rotate 4 Answers

Trouble rotating a character to correctly match new gravity direction 0 Answers

transform.Rotate slows down my moving cube 0 Answers

Clamping X rotation on camera. 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