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 stuthemoo · Sep 17, 2011 at 01:07 AM · inputrotatereleaserevert

Rotate to 0

Hi,

I have a spaceship that rotates on the z axis with a max of 30 degrees when the Horizontal axis is held down to give the effect of roll like an airplane. How can I make my object rotate back to 0 degrees when the Horizontal axis is released? So the object only rotates while the left/right keys are held and then it slowly rotates back to 0 when these keys are let go?

Cheers.

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

Answer by syclamoth · Sep 17, 2011 at 02:10 AM

How about this- instead of using the Horizontal axis directly, have the Horizontal axis define a "target roll"- this determines how far you want the spaceship to bank. Then in your Update function interpolate between your current roll and the target roll using a function like Mathf.Lerp or Vector3.Lerp. Or, better still, you could weight it so that the interpolation amount changes depending on how much of the axis is pressed- so that if the axis is at full lock it interpolates fast, and if the axis is at zero it interpolates slowly, something like this-

 // This is the maximum angle that your ship will rotate to (assign it in editor)
 public float maxAngle = 30;
 // This is the stored angle that your ship is at
 float currentAngle = 0;
 
 void Update()
 {

     // This makes your angle somewhere between -30 and 30 degrees
     float targetAngle = Input.GetAxis("Horizontal") * maxAngle;
     // This makes the interpolation faster when the input is pressed down,
     // making sure that the value is always positive.
     float interpolationSpeed = 1 + (Mathf.Abs(Input.GetAxis("Horizontal") * 10));

     // This smoothly sets the current angle based on the input
     currentAngle = Mathf.Lerp(currentAngle, targetAngle, interpolationSpeed);

    // replace this with however you implement the final value
    transform.rotation = Quaternion.Euler(0, 0, currentAngle);
 }
Comment
Add comment · Show 8 · 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 stuthemoo · Sep 18, 2011 at 01:42 AM 0
Share

Thanks syclamoth! Only problem is that it will only rotate in one direction currently, I need to set the target angle as -30 but I can't figure out how to make this work?

avatar image syclamoth · Sep 18, 2011 at 04:33 AM 0
Share

Input.GetAxis("whatever") should return a value between 1 and -1. Have a look at how your axis is set up, and see if there's anything limiting its rotation!

avatar image Bunny83 · Sep 18, 2011 at 04:36 AM 0
Share

@stuthemoo: Use comments if you need further information. The Answer button is dedicated for actual Answers to the question. If you have another question you should ask a new seperate question. If you need more help with this question use comments or edit your question to improve it.

avatar image stuthemoo · Sep 18, 2011 at 09:12 AM 0
Share

No, my Horizontal axis is set up fine and works for the movement of the spaceship. When I use log.debug to show the value of targetAngle i am getting negative numbers one way and positive numbers the other. Are you sure theres nothing in the script that could be causing this?

Sorry Bunny83 that was an accident :/

avatar image syclamoth · Sep 18, 2011 at 09:43 AM 0
Share

Put Debug.Log lines inbetween every line of code- this way you can find out exactly what is happening everywhere! I don't know why it isn't working properly- unless I had the code right in front of me, I can't work out what the problem is. How are you implementing the final value?

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2D rotation around hinge joint with joystick input 0 Answers

How to alter input to continuous rather than tap? 0 Answers

How to prevent Z axis rotation ? 1 Answer

Space Camera Issue. 1 Answer

How to alter input to continuous rather than tap? 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