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 Adam_Falkejon · Jun 06, 2018 at 02:39 PM · rotationrotaterotate object

How to return gameobjects rotation to original Rotation / zero rotation?

,I've managed to make the object rotate like I want, except that it doesn't go back to it's original rotation when I let go off the buttons. I'm using Input.GetAxis incase I wanna try it out with a gamepad, I couldn't find any solution for it while using that. I saw some solutions when using normal Input.GetKey.

I don't know if you need to see the code but here it is:

     rwfRotation -= Input.GetAxis("Vertical");
     rwfRotation = Mathf.Clamp(rwfRotation, -2, 4);
     RightWingFlap.transform.localEulerAngles = new Vector3(rwfRotation * 5, RightWingFlap.transform.localEulerAngles.y, RightWingFlap.transform.localEulerAngles.z);

This makes the flaps turn up and/or down when I press either S or W. I want the flap to return to it's original rotation when I'm not pressing S or W. The variable name might be a bit weird, but trying to make a airplane, with flaps :)

Comment
Add comment · Show 1
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 Harinezumi · Jun 06, 2018 at 02:56 PM 0
Share

Store the original rotation of the game object, and when you are not giving any input, return to that value.

A more sophisticated approach is to encapsulate this into a component, that stores its original local rotation in its Start() method, and ins$$anonymous$$d of directly controlling its rotation, you create functions that tell it that you want it to increase or decrease rotation, and store that value as desiredRotation = currentRotation + change. Then in Update() you limit the desiredRotation (like $$anonymous$$athf.Clamp(), and use Quaternion.Lerp() to go towards the desiredRotation, so it will achieve it over time. Finally, at the end of Update() you set desiredRotation to originalRotation, so it will always want to return to it when there is no input.

1 Reply

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

Answer by tormentoarmagedoom · Jun 06, 2018 at 03:26 PM

Good day.

You just need to store the original position and rotation. Does not care how you move/rotate it, or what inputs are you detecting.

If at the begining of your script, at the Start, you store the position and rotation like this:

 Vector3 InitialPosition;
 Quaternion InitialRotation;

 void Start()
 {
 InitialPosition = gameObject.transform.position;
 InitialRotation = gameObject.transform.rotation;
 }

Then, simply when you want it to come back (maybe when some Input is null or i dont know) you need to change its position an rotation again

 gameObject.transform.position = InitialPosition;
 gameObject.transform.rotation = InitialRotation;

Bye!

Comment
Add comment · Show 5 · 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 Getsumi3 · Jun 06, 2018 at 03:29 PM 0
Share

Agree This is the correct answer

avatar image Adam_Falkejon · Jun 06, 2018 at 03:43 PM 0
Share

I think I solved it, I had to move it to update, since It's a part on my plane that I'm rotating, when I had it in start it always wanted to rotate back to like start value, not caring which direction the plane is in. But moving it to update seemed to solve it, now I just need to figure out how to make it when there is no input. Anyone got an idea about that? How to recognize no input for Input.GetAxis.

avatar image Harinezumi Adam_Falkejon · Jun 06, 2018 at 03:50 PM 0
Share

Check out my comment: have desiredRotation, and reset its value to originalRotation at the end of Update().

avatar image Adam_Falkejon Harinezumi · Jun 06, 2018 at 03:53 PM 0
Share

Yea I saw your comment, I just didn't really understand, sorry. I'm not that experienced.

avatar image Adam_Falkejon · Jun 06, 2018 at 03:49 PM 0
Share

if (Input.GetAxis("Yaw") >= -0.1f && Input.GetAxis("Yaw") <= 0.1f) { Rudder.transform.rotation = OriginalRotation; }

kinda works... problem is that, it stores the old rotation value, so that when I give a input after it has returned to 0, it jumps back to the old rotation and then starts from there, and not from 0

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

121 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

Related Questions

gameobject stops moving correctly when rotating 1 Answer

Smooth Camera Rotation After 2 Seconds 1 Answer

transform.Rotate will not work 1 Answer

Wonky Camera Behaviours 0 Answers

How do I rotate an object to the cursor?Where is the mistake in the code? 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