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 /
  • Help Room /
avatar image
1
Question by RealoFoxtrot · Apr 29, 2017 at 07:01 AM · 2d rotation

Unity2D analogue stick rotation?

So i am using Unity2D with a load of Vector2s, and want to gradually rotate the character to aim in the direction the analogue stick is pointing.

However, when using my current code, as soon as the turn script is accessed, the character disappears. After some experimentation, I discovered that the character had snapped 90 degrees and was now balanced perpendicular, like paper mario.

I suspect there is a problem somewhere with Vector2 being x y, and so it wont quaternion around z

 void FixedUpdate()
 {
 
         float TurnV = Input.GetAxis("TurnV");
         float TurnH = Input.GetAxis("TurnH");
 
         aimDirection = new Vector2(TurnH, TurnV);
 
 
         if (TurnV != 0 && TurnH != 0)
 
         {
             catBody.transform.rotation = Quaternion.LookRotation(aimDirection);
         }
 
 }

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 RealoFoxtrot · Apr 29, 2017 at 07:22 AM 0
Share

This is a Gfy pf what happens when i activate it. Upon exa$$anonymous$$ation, the weirdness is more apparent. It starts bouncing, and gravity points back at the origin

1 Reply

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

Answer by RealoFoxtrot · Apr 29, 2017 at 02:17 PM

Okay, my solution found from around the internet was to use Mathf.Atan2

Ends up looking like this:

 void FixedUpdate()
 {
         aimAngle = Mathf.Atan2(TurnH, TurnV) * Mathf.Rad2Deg;
 
             if (TurnV != 0 && TurnH != 0)
 
             {
                 catBody.transform.rotation = Quaternion.AngleAxis(aimAngle, Vector3.forward);
             }
 }

There are a few extra things, like having a check for the character being in the air, and catbody is the name of the rigidbody2d attached.

it's not perfect yet. I actually want to lerp between the current angle, and the analogue stick angle. But for a start, this works

=========== Edit:

Okay, I have already solved the lerp issue. Will try to include all the relevant code now:

     protected Rigidbody2D catBody;
     protected Vector2 aimDirection; //direction the cat is pointing, jump direction
     protected float aimAngle;
     protected Quaternion aimRotation;
 
     void Start ()
     {
         catBody = GetComponent<Rigidbody2D> ();
     }
 
 
     void FixedUpdate()
     {
         float TurnV = Input.GetAxis("TurnV");
         float TurnH = Input.GetAxis("TurnH");
 
         aimDirection = new Vector2(TurnH, TurnV);
         aimAngle = Mathf.Atan2(TurnH, TurnV) * Mathf.Rad2Deg;
 
 
             if (TurnV != 0 && TurnH != 0)
 
             {
                 aimRotation = Quaternion.AngleAxis(aimAngle, Vector3.forward);
                 catBody.transform.rotation = Quaternion.Slerp(catBody.transform.rotation, aimRotation, turnSpeed * Time.time);
             }


Outside of the code, the TurnV and TurnH input deadzone needs to be set to 0.03

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 SepM · May 06, 2018 at 05:48 AM 0
Share

Thanks! This helped me turn a character naturally with analog stick movement. The only thing I did differently in my case was use Vector3.up ins$$anonymous$$d of forward to turn the character as he moves. Thanks for the great solution!

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

99 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

Related Questions

strange rotation in 2d 0 Answers

Enemy's projectile is firing at the opposite direction when it's y-axis align with the player's 0 Answers

how do i make my sprite turn to face the direction that my cursor is moving? 0 Answers

How to fix spite character changing position when turning? 1 Answer

How do you instantiate an object at the tip of a rotating object? 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