• 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 unity_w108xjlENsUk1Q · Apr 28, 2020 at 05:18 PM · rotateangle

How to rotate an object along the Y axis to the right, and then to the left?

How to rotate an object along the Y axis to the right, and then to the left by a certain number of degrees. The object moves and rotates randomly, so the starting angle can be any.

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
0

Answer by LuigiDreemurr · Apr 28, 2020 at 05:58 PM

 void Update
 {
 
     float y = 5; //easy variable for y
 
     this.gameObject.transform.localEulerAngles = new Vector3(0, y, 0);
 
 }

This changes the local y angle to 5 degrees, you can also just use EulerAngles for worldspace angles instead of local. As for wanting to change it I assume you want it over time otherwise its gonna twitch out. I recommend using an IEnumerator for that if its always running else Update will work fine, just be sure to set a speed and multiply it by DeltaTime. Deltatime ensures a smooth transition over time, otherwise your code runs once a frame and any transforms will be done way to fast.

 private float y = 0; //we need to store the variable outside so to not have it reset every time update is called.
 public float rotationSpeed = 5; //This is a public variable that we can change for preferred rotation speed.
 public float desiredAngle = 30;
 
 void Update
     {
 
         //This will increase the angle over time
         y += rotationSpeed * Time.deltaTime;
         //This changes the local Euler angle(Standard degrees) of y while keeping x and z at 0
         this.gameObject.transform.localEulerAngles = new Vector3(0, y, 0);
 
         If (y > desiredAngle || y < 1) //I do this to avoid getting gimble locked
              rotationSpeed = -rotationSpeed ; //and with a simple negitave switch we change the direction of the rotation.
     
     }

Hope this answers all of your current questions! Happy coding!

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 unity_w108xjlENsUk1Q · Apr 28, 2020 at 10:26 PM 0
Share

If rotation.y is not zero, when the code starts to execute, the object takes the coordinate rotation.Y at 0. How to make the object start rotation from the current position along the y axis?

avatar image
0

Answer by Tario · Apr 28, 2020 at 05:34 PM

Sine wave.

Mathf.Sin(x); outputs a value that will go from zero, up to one, then down to -1 and back to zero at a constant, smooth rate as you increase the value of x. Use that, along with the Quaternion.Euler(); method.


Want more than 1 degrees change? No problem! Multiply the output of Mathf.Sin(x); by the maximum number of degrees you want. At 1, you'll have all your degrees, at 0, you'll have none and at -1, you'll be looking the other direction.


Alternatively, look into Mathf.Pingpong();

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

131 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

Related Questions

How to know 3DGameObject rotating clockwise or counterclockwise 1 Answer

How to rotate an object without using a timer 1 Answer

How to rotate a character facing direction of movement like sonic the hedgehog 2 Answers

Rotate camera to character angle 0 Answers

How to rotate a dice to a final angle ? 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