Rotate and move character with Joystick

How can I rotate my player (Panzer) with Joybutton before moving to another direction. Moving the player works well but problem is its moving horizontally and I want to rotate the player to its moving direction. My code is as below. Thanks.

Source code

assuming your joystick inputs are relative to the main camera…

  1. keep a reference to the Camera’s transform in a variable… camTran

  2. now you can generate a vector based on the camera’s camTran .forward and camTran.right multiplied by the joystick inputs
    e.g. Vector3 joyFacingVector = ((camTran.forward * joy.Y) + (camTran.right * joy.X)).normalized;
    note:- if your joystick is centered… with magnitude 0, then your joyFacingVector needs to be just camTran.forward

  3. now you can rotate the tank towards that vector.
    Vector3.rotateTowards(tank.transform.forward, joyFacingVector, maxAngleDelta, maxMagDelta);
    Unity - Scripting API: Vector3.RotateTowards

Note:- for the turret… you can face that forwards after rotating the base… so it still shoots forwards e.g.
4) Turret.transform.forward = camTran.forward;
but that all depends what you want from your controls :slight_smile:

its a bit late

but i did it like this

float angle = Mathf.Atan2(joy.Horizontal, joy.Vertical) * Mathf.Rad2Deg;
this.transform.rotation =Quaternion.Euler(new Vector3(0, angle, 0));