• 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
Question by Selosoft · Apr 17, 2012 at 03:52 AM · rotationmovementrotateangleroll

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

Hey guys, my title pretty much sums it up. I've been stuck on this forever now. I keep breaking and coming back to it but I really want to solve this. Hopefully someone can help me out. I want to consistently rotate my character like a constant forward roll in his direction of movement, just like in sonic the hedgehog. I've got his direction of movement working right and his roll movement working but I can't for the life of me figure out how to have him face his move direction while he rolls forward. Obviously I can't use transform.LookAt because he's consistently rolling and his LookAt point will change constantly. Here is what I've got... any ideas?

     #pragma strict
 var rotateSpeed: float = 60;
 private var lastPos: Vector3;
 
 function Update(){
 var moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,0); 
 var dist = transform.position - lastPos;
 lastPos = transform.position;
 var angle = dist.x * rotateSpeed;
 
 
 if(moveDirection.x > 0){
 print("moving right");
 transform.Rotate(angle, 0, 0);
 }
 if(moveDirection.x < 0){
 print("moving left");
 transform.Rotate(-angle, 0, 0);
 }
 }
Comment

People who like this

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

Answer by Lttldude · Apr 17, 2012 at 04:04 AM

Maybe make him the child of an empty gameobject, and then the character can rotate and stuff in local space, while the parent defines the facing direction.

Comment
Selosoft

People who like this

1 Show 0 · 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

Answer by Selosoft · Apr 17, 2012 at 04:21 AM

lttldude9 you made my night. I can't believe the solution was so simple. I still gotta tweak some stuff but yup this totally works.

feel free to post an answer and ill +thumb up it

Comment

People who like this

0 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 Lttldude · Apr 17, 2012 at 02:26 PM 0
Share

I'm new to answers.unity3d.com and never asked a question before, but I think when someone answers your question correctly you are suppose to actually select that answer as correct. From the FAQ: "Click the check-mark icon to make it green." Idk, tell me if that makes sense.

avatar image sonicjason255 · Dec 02, 2017 at 12:22 AM 0
Share

I Have been looking for something like this forever. thank you.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trouble with Vector3.Angle() and RotateAround() 1 Answer

How to rotate Character depending on the inclination of the platform or its parts? 1 Answer

gameobject stops moving correctly when rotating 1 Answer

How do I rotate a game object and have it move in relation to the rotation angle? 2 Answers

How can I move an object to click point in 2D? 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