Hello, I’m want to add Joystick controls to my 2d game.
But my problem is that I don’t know how rotate the player object to the direction where the player is going.
Here is my current script:
private void MovePLayer(Vector3 InputDirection) {
if(joystick.Direction != null)
{
transform.Translate(new Vector2(InputDirection.x * AccelerationSpeed, InputDirection.y * AccelerationSpeed) * Time.deltaTime);
}
}
You may be able to simply drive transform.forward
(or transform.up
when in 2D) equal to your movement vector.
Otherwise, use Mathf.Atan2() to derive heading from cartesian coordinates:
Sine/Cosine/Atan2 (sin/cos/atan2) and rotational familiarity and conventions