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 :smiley:

      //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.

37448-example.png

Thank you.

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;
}

untested :wink:

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

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.