Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Youngapprentice · May 05, 2012 at 10:30 PM · rotationsimple

Temporary Rotation

Hi, guys! I have a very short question. I would like to have an object rotate upwards, downwards, left, and right when I press the respective keys.

2 issues with this though: 1. I'd like to set boundaries for how far it can rotate 2. I'd like to make it go back to it's original rotation after I let go of a key. But not suddenly. As in I'd like it to fade back. I think that's done with Lerps or something.

Please help! I am utterly confused when it comes to the world of rotations. :)

Thanks!- YA

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

Answer by Youngapprentice · May 05, 2012 at 11:20 PM

Nevermind I figured it out. Here is the script:

     // Smoothly tilts a transform towards a target rotation.
 var tiltAngle = 30.0;
 var MoveTiltspeed = 5.0;
 function Update () {
     var tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
     var tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
     var target = Quaternion.Euler (-tiltAroundX, 0, -tiltAroundZ);
     // Dampen towards the target rotation
     // Dampen speed is movement speed * 1.2 so that they stay in proportion realistically
     transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * MoveTiltspeed*1.2);;
     var x = Input.GetAxis("Horizontal") * Time.deltaTime * MoveTiltspeed;
     var y = Input.GetAxis("Vertical") * Time.deltaTime * MoveTiltspeed;
     transform.Translate(x, y, 0, Space.World);
 }
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
avatar image
1

Answer by kolban · May 05, 2012 at 11:14 PM

Within a Script, a function called Update() is called every frame. Within this Update function, one can code a call to Input.GetKey(). This will return true if a specific key is pressed. Based on this, you now have knowledge that a key has been pressed. Now let us talk about rotation. The rotation of a Game Object can be determined from its Transform component. This is stored in the transform.rotation property. One should save the start value of this property when NO keys are pressed. When a key is pressed, one can then start changing the value of this property based on the key ... for example, the up arrow rotates around the X axis.

You want to consider the concept of the rate of rotation. Think of this as a value measured in degrees per second. For example, if you want to rotate at 10 degrees per second, you would rotate by 10*Time.deltaTime within the Update function. The Time.deltaTime is the number of seconds since the last time Update was called.

When no keys are pressed, you want to return to your original rotation over a period of time. You can do this using the Vector3 Lerp function again using Time.deltaTime as a rate.

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 Youngapprentice · May 05, 2012 at 11:21 PM 0
Share

Thank you, sir!

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

C# Unity 3D Lock Rotation but allow Rotation of Parent Object 1 Answer

How can i make my cube not be a 3D parallelogram when i rotate it under its parent? 1 Answer

What is wrong with this script? 2 Answers

problem with my simple script please help. 2 Answers

Attaching an RTS scrolling script to a camera (Noob Question) 3 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