• 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 ahmadian · Jan 08, 2017 at 03:13 PM · android2drotationjoystick

How to control a 2d character in an top-down android game with the standard joystick?

So I'm building a 2d top-down game and I need to control my character using unity cross-platform standard asset single joystick.

The problem is with the rotation, I don't want to use the dual-stick for some idea related reasons and the designs and the game mechanics that was considered for this game.

So I want the character to have the same rotation as the joystick and since the joystick only has the horizontal and the vertical axies it's hard to get a descent rotation.

So any ideas how to do this? if you need further clarification let me know.

Comment
Add comment · Show 2
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 hoekkii · Jan 08, 2017 at 03:44 PM 1
Share

You can solve this in different ways. you can use Atan2(x, y) for the angle of the joystick and set the y angle of the player. or if you also move you can do something like Quaternion.LookDirection(your direction, up).

avatar image ahmadian hoekkii · Jan 09, 2017 at 05:20 AM 0
Share

Atan2(x,y) Fixed my problem thank you. I didn't know what it does so I ignored it.

1 Reply

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

Answer by ahmadian · Jan 09, 2017 at 05:54 AM

as @hoekkii said, in the mathf class there is a function called Atan2, which is exactly what is needed here.

So all that is needed to be done is to use the mobile single stick control in the cross-platform standard assets and get the Horizontal and Vertical axis from that and use the Atan2 to get the radiant and then turn it to degrees and use it to set the z angle of the player.

Player.transform.eulerAngles = new Vector3(0, 0, -(Mathf.Atan2(CrossPlatformInputManager.GetAxis("Horizontal"), CrossPlatformInputManager.GetAxis("Vertical")) * Mathf.Rad2Deg));
or for simplicity :
float Haxis = CrossPlatformInputManager.GetAxis("Horizontal");
float Vaxis = CrossPlatformInputManager.GetAxis("Vertical");
float Zangle = Mathf.Atan2(Haxis, Vaxis) * Mathf.Rad2Deg;
Player.transform.eulerAngles = new Vector3(0, 0, -Zangle);

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 noeyedeer321 · Oct 30, 2019 at 12:21 AM 0
Share

Thanks a lot I've been trying to solve this for over 3 days.

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

151 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 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

2D mobile: Need my character to rotate Z axis towards touch position 0 Answers

Android based ARCore example ObjectManipulation using Joystick Controller 0 Answers

Joystick move Left Right and down, plus rotation. 0 Answers

Move towards VR Camera Orientation with Bluetooth Controller (Android) 0 Answers

Rotate the weapon sprite with a joystick 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