- Home /
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.
Thank you.
I am having the same issue and cannot find an example code anywhere. Can someone please direct me to an example of this?
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.
@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
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 ;)
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!
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
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!
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
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.
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.
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.
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 :)