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 /
avatar image
0
Question by Hamsterlings · Dec 22, 2014 at 02:54 PM · rotationmobilejoystick

Make gameObject face position of Joystick [Mobile]

After hours of head scratching and different combinations from googling, I give up!

I'm trying to write joystick for Android to use in my game. It's double joystick - left stick will move character, right stick will rotate it.

I have set up UI with new system from 4.6 so I have OnPress and OnDrag events and handlers. I'm moving whole button using Touch input position - that's working fine.

So now I want to rotate character in direction where thumbstick is being dragged.

If I understood, I need to pull through Mathf.Atan2 x and y values of position of thumbstick, then multiply this with Rad2Deg which would give me workable angle that I can use, however I couldn't set it up properly and I never achieved result.

I tried many things and I don't have working code but it should be along those lines :

 var angle = Mathf.Atan2(touchPosition.x, touchPosition.y);// *Mathf.Rad2Deg;
 
 PlayerGO.transform.Rotate(0, angle, 0);

Anyway, here is mess of a code of all different variations I've tried - I really did try :D

       //var touchNormalx = touchPosition.x;//Mathf.Clamp(touchPosition.x, -1f, 1f);
           //var touchNormaly = touchPosition.y;//Mathf.Clamp(touchPosition.y, -1f, 1f);
 
           // var angle = Mathf.Atan2(touchPosition.x, touchPosition.y);// *Mathf.Rad2Deg;
 
            //float cramp = Mathf.Clamp(touchPosition.x, -1f, 1f);
            //float cramp2 = Mathf.Clamp(touchPosition.y, -1f, 1f);
            //float angle = Mathf.Atan2(cramp, cramp2) * Mathf.Rad2Deg;
 
           //  PlayerGO.transform.rotation *= Quaternion.AngleAxis(angle, Vector3.up);
             
             //PlayerGO.transform.eulerAngles = new Vector3(PlayerGO.transform.eulerAngles.x, Mathf.Atan2(touchNormalx, touchNormaly) * Mathf.Rad2Deg , PlayerGO.transform.eulerAngles.z);
             //PlayerGO.transform.RotateAroundLocal( Vector3.up, (Mathf.Atan2(cramp, cramp2) * Mathf.Rad2Deg));
             //PlayerGO.transform.Rotate(0, angle, 0);
             //PlayerGO.transform.LookAt(new Vector3(0, angle, 0));


Of course, I didn't use all lines, but tried various options.

Anyway, to sum up - I want my character to face direction of joystick dot when player moves it. I don't want it to add to rotation - I just want it to keep setting rotation of character to rotation of touch angle.

Here is an example : Red dot is center of thumbstick. Arrows show direction. Blue dot is touch position. On the right you can see green dot - player, and red arrow, showing direction where player should look. Also, I want this to be dynamic - as in to work with actual position of touch, not just basic transform rotation.

alt text

Thank you.

example.png (18.9 kB)
Comment
Add comment · Show 4
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 Mokhan94 · Oct 22, 2015 at 10:54 PM 0
Share

I am having the same issue and cannot find an example code anywhere. Can someone please direct me to an example of this?

avatar image meat5000 ♦ Mokhan94 · Oct 22, 2015 at 10:56 PM 0
Share

I wrote for Android, so a visual stick is present on screen.

I made animations for Walk/Run forward, backward and turn left, turn right.

Stick.Y = Walk

Stick.X = Turn.

So normally, up on stick makes character walk forward no matter his facing direction. This is your link between Camera and Joystick. Up on stick and Forward on Camera correspond to 0 degrees.

The formula goes

Jr = -P + Jp

(Jr = Joystick Rotation Required, P = Player rotation from camera, Jp = Joystick Press Rotation)

So, find clockwise rotation of Player (P) with respect to camera forward. $$anonymous$$ake it Negative. Use AngleAxis to create a rotation of this angle around Vector3.up. $$anonymous$$ultiply the Quaternion by your input stick vector.

Note, mapping Stick x,y to Vector3 x,z helps with simplistic Vector2 rotation. Also one will require the use of Vector3.cross to find Angles over 180 degrees.

avatar image yozelectric · Nov 25, 2015 at 03:18 PM 0
Share

@Hamsterlings i am now in this issue :)

and as far as I think, i will try to implement arcsin or arccos, or arctan of the value we know that the value of joystick (vertical or horizontal) is 0 to 1, and arcsin of "1" is 90 , its the only way to convert the value into the angle...

i will try this in some hours

avatar image meat5000 ♦ yozelectric · Nov 25, 2015 at 03:24 PM 0
Share

Just use Vector2

3 Replies

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

Answer by AlucardJay · Dec 22, 2014 at 05:22 PM

What are the values touchPosition.x and touchPosition.y returning? Float values between -1 and 1? or any directional vector relative to the stick center?

If so :

 // Get a Directional Vector from the Joystick input / offset from center
 Vector3 targetDirection = new Vector3( touchPosition.x, 0f touchPosition.y );
 
 // Quaternion.LookRotation logs an error if the forward direction is zero.
 // check if targetDirection is NOT Vector3.zero
 if ( targetDirection != Vector3.zero )
 {
     Quaternion targetRotation = Quaternion.LookRotation( targetDirection, Vector3.up );
     player.transform.rotation = targetRotation;
 }

http://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html

untested ;)

Comment
Add comment · Show 3 · 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 Hamsterlings · Dec 22, 2014 at 05:42 PM 0
Share

touchPosition just returns value from actual touch:

 Vector3 touchPosition = Input.GetTouch(0).position;

It's not relative to the stick center, any suggestion on that ?

Code doesn't work as expected but it's for sure closest thing to working code since I started coding today :)

Thanks!

avatar image AlucardJay · Dec 22, 2014 at 05:54 PM 1
Share

How are you positioning/moving each stick from touch inputs then? Recognizing what stick is being touched/dragged, and where the touch is in relation to each sticks center is essential input management. All you want from your joystick script are horizontal and vertical inputs (just like Input.GetAxis("Horizontal") etc) . As Hamsterlings mentioned, there is a Unity asset that will manage this for you. Check out the Sample Assets Beta.

Edit : tried to find an old link to explain better with no luck, But I found this video (not watched, but seen others by Devon Curry and recommend watching anyway!) : https://www.youtube.com/watch?v=ZGvkaHHQD7c

avatar image Hamsterlings · Dec 23, 2014 at 10:21 AM 0
Share

Thank you so much alucardj, in my and in name of others whom you helped!

It's not problem for me to google or watch videos, I had no idea how to realize this into working code.

With that said, you helped me to write script and get two joysticks for movement and rotation how I wanted it to be :)

I have two new problems though : 1. I can't get unity to recognize second touch with new 4.6 buttons 2. When I use rotation stick my movement stick loses itself.

I will mark your answer as an valid answer and I will post my script for everyone else to see.

If you can help with some tip or suggestion for two remaining problems, please let me know!

avatar image
2

Answer by yozelectric · Nov 27, 2015 at 11:16 AM

i have done it. @Hamsterlings @meat5000

you can use @meat5000 method using vector2 or my method as this (monster as the GameObject):

         if ( CrossPlatformInputManager.GetAxis("Vertical")>0 )
                 monster.transform.eulerAngles = new Vector3( 0, Mathf.Asin(CrossPlatformInputManager.GetAxis("Horizontal")) * (180/Mathf.PI), 0 );
     
         if ( CrossPlatformInputManager.GetAxis("Vertical")< 0)
                 monster.transform.eulerAngles = new Vector3( 0, 90 + (Mathf.Acos(CrossPlatformInputManager.GetAxis("Horizontal")) * (180/Mathf.PI)), 0 );

vertical and horizontal input of joystick in standard asset is in range of -1 to 1.

asin and acos returns angles. you should check on trigonometry table, which value do you want to use to convert:

"the float from 0 to 1 from joystick value ---into---> rotation angle/eulerangle."


since the angle is in "Radians", and you have to convert it to "Degree" by doing:

degree = rad_value * (180/mathf.PI)


WonderfulIndonesia

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 ieselisra · Feb 02, 2016 at 09:46 PM 0
Share

Easy way to do it. Worked perfectly.

avatar image
1

Answer by screenname_taken · Dec 22, 2014 at 03:00 PM

I did this in a workaround way. I have an empty game object that the player is supposed to always be looking at. Then the thumb stick is moving that thing at -1,1 X and Y axis, so it goes +1 when the joystick points right and similarly for the rest of the axis... stuff. Now since the player object is looking at it, it rotates to whereever the object it.

I know that there is a more elegand way (there has to) but i'm not that smart.

Comment
Add comment · Show 3 · 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 Hamsterlings · Dec 22, 2014 at 04:54 PM 0
Share

Would you care to share some code for this ? I get the idea but I'm not completely sure how to implement it to be honest, it would be a lot easier if I had some code sample.

Also, as long as I don't hit performance I don't really care if it's workaround or not :)

Thank you.

avatar image screenname_taken · Dec 22, 2014 at 05:32 PM 0
Share

I was just using Unity's joystick JS script and i was getting the values from it and feeding it directly in the object's transform.

avatar image Hamsterlings · Dec 23, 2014 at 10:22 AM 0
Share

I'm not using unity's script but rather writing my own from scratch, I also don't have access to beta standard assets - new Unity freezes when I try to open store. I gave you thumbs for trying to help of course, 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.

Follow this Question

Answers Answers and Comments

32 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

Related Questions

Single Joystick Movement 0 Answers

How can i rotate 3D object with joystick in all directions? 0 Answers

Rigidbody rotation with virtual joystick, weird behaviour 0 Answers

why is my character rotation always 180° after joystick is released. 1 Answer

Rotate a GameObject in the Vector3 direction? 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