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 /
This question was closed Jul 03, 2014 at 01:39 PM by ado112 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ado112 · May 05, 2014 at 08:10 PM · androiddistancejoystickangle

How do i get joystick angle and distance from its origin place?

tried this but not working:

 var joystick : Joystick; //part of the script.
 var V1 : Vector2;
 var V2 : Vector2;
 private var P : Transform;
 
 function Start () {
 V1 = new Vector2(joystick.position.x,joystick.position.y);
 V2 = new Vector2(P.position.x,P.position.y);
 }
 
 function Update () {
 P.position.y = joystick.position.y = 0;
 P.position.x = joystick.position.x = 0;
 var Di = Vector2.Distance(V1,V2);
 var An = Vector2.Angle(V1,V2);
 transform.rotation.y = An;
 transform.position.z += Di;
 }
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

  • Sort: 
avatar image
3
Best Answer

Answer by wibble82 · May 05, 2014 at 09:33 PM

The magnitude of the position (joystick.position.magnitude) is effectively it's distance fom it's centre.

The angle can be calculated using the atan2 function: angle = Mathf.ATan2(pos.x,pos.y)

However that function gives you an angle in radians. To get it in degrees, multiply it by Mathf.Rad2Deg

Comment
Add comment · Show 4 · 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 ado112 · May 06, 2014 at 05:05 PM 0
Share

not working

avatar image wibble82 · May 06, 2014 at 05:24 PM 0
Share

The maths works! I'd need to see your exact code to know why it isn't doing the job. Or even any info at all! Help us help you basically - just saying "not working" doesn't help.

avatar image ado112 · May 06, 2014 at 07:14 PM 0
Share

i made a little code fixes and everything works fine.

avatar image farragar · Jul 08, 2021 at 09:23 AM 0
Share

For anyone landing on this in 2021, the function is now Mathf.Atan2(x, y) - Note that the 'T' in tan, is lowercased.

avatar image
3

Answer by Jeff-Kesselman · May 05, 2014 at 09:01 PM

Use Input.GetRawAxis() to get number between -1 and positive 1 for each axis. Multiply that by the maximum deflection angle for your stick.

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 ado112 · May 05, 2014 at 09:17 PM 0
Share

I saw that input.GetAxisRaw needs string variables, joystick has only floats , im making game for phones(android).

Follow this Question

Answers Answers and Comments

23 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to make a menu like ironpants 0 Answers

Reloading the scene causes ads to reload. Is this ok ? 3 Answers

joystick left right problem unity 0 Answers

Android Joystick Control without CharacterController 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