How to Rotate Y axis of an Object toward virtual joystick direction
Hi
I'm creating a third-person game which uses virtual analog/joystick. I have a player and i want it rotate toward the virtual joystick direction. I'm a beginner and i don't know so far about this. But, I have an idea by creating a cube in the middle of analog image as a director then set the rotation of the cube to look at the virtual joystick. This is good but I get the X axis while I need the Y axis since my game is a 3D Third-person. Here is my code so far:
public virtual void OnDrag(PointerEventData ped)
{
// another code
var relativeUp = joystickImg.transform.TransformDirection (Vector3.forward);
var relativePos = joystickImg.transform.position - director.position;
director.rotation = Quaternion.LookRotation (relativePos, relativeUp);
player.transform.rotation = Quaternion.Slerp(player.transform.rotation, director.rotation, 5 * Time.deltaTime);
}
I really have no idea to swap the X axis of director rotation to Y axis of player rotation. I've tried something and changed the codes like this:
public virtual void OnDrag(PointerEventData ped)
{
// another code
var relativeUp = joystickImg.transform.TransformDirection (Vector3.forward);
var relativePos = joystickImg.transform.position - director.position;
director.rotation = Quaternion.LookRotation (relativePos, relativeUp);
var directorRot = director.rotation;
directorRot.y = directorRot.x;
directorRot.x = player.transform.rotation.x;
directorRot.z = player.transform.rotation.z;
player.transform.rotation = Quaternion.Slerp(player.transform.rotation, directorRot, 5 * Time.deltaTime);
}
But that doesn't work, the player rotate to a wrong direction and then stop after it reach some angle (won't rotate). I think now I need the swapping method. Anyone knows how? Thanks!