• 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 Z3uS · Jul 07, 2015 at 03:09 AM · rotationangle

Rotating a 2D sprite

I want to rotate a 2D sprite based in its velocity, and currently I am using the following code:

 void FixedUpdate () 
 {
   rigidbody2D.rotation = -rigidbody2D.velocity.x * 5;
 }

The problem with this is as soon as I leave the direction key, it does not return to original state and stays at the same angle. Can any suggest any changes in the code to do the same?

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

Answer by PatrikL · Jul 07, 2015 at 12:13 PM

It sounds as if you want it to rotate only when your character is moving. In that case you could multiply your value by the input direction.

 void FixedUpdate () 
 {
     rigidbody2D.rotation = -rigidbody2D.velocity.x * 5 * Input.GetAxis("Horizontal");
 }
 

It might be the case where the input value changes to rapidly, and you want it to look smoother. Then you could ease the value using the Lerp function.

 void FixedUpdate () 
 {
     rigidbody2D.rotation = Mathf.Lerp(rigidbody2D.rotation, -rigidbody2D.velocity.x * 5 * Input.GetAxis("Horizontal"), Time.deltaTime * 10f)
 }

The value "10f" indicates how fast it should easy. Higher equals faster.

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 Z3uS · Jul 07, 2015 at 05:51 PM 0
Share

thanks a ton man, it was exactly what i needed

avatar image Z3uS · Jul 07, 2015 at 05:54 PM 0
Share

could you also any way to do it using touch controls. I am using swipe to control the character.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to use the lerp function with rotation? (C#) 1 Answer

How do I add force to an object based on my Camera's cureent Y rotation (C#) 2 Answers

rotation x changes all angles? 3 Answers

Set rotation based on 2 points problem 1 Answer

local rotation per axis between frames? 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