How can I get movement like this?

  • I would love to make a tank game with this sort of movement does anyone know how to get this kind of movement? This is the script I have currently but it doesnt act like tank movement

public class PlayerMovement : MonoBehaviour {

public float moveSpeed;

// Use this for initialization
void Start () {
    moveSpeed = .5f;
}

// Update is called once per frame
void Update () {
    float moveHorizontal = Input.GetAxisRaw("Horizontal");
    float moveVertical = Input.GetAxisRaw("Vertical");

  
    

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    if (movement != Vector3.zero) transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement.normalized), 0.05f);

    transform.Translate(movement * moveSpeed * Time.deltaTime, Space.World);
}

}

Since you want the object to rotate independently of its forwards and backwards movement. you need to handle rotation and movement separately.

check when each movement is happening and apply movement accordingly to each behavior.

  public float moveSpeed;
    // Use this for initialization
    void Start()
    {
        moveSpeed = .2f;
    }

    // Update is called once per frame
    void Update()
    {
        float moveHorizontal = Input.GetAxisRaw("Horizontal");
        float moveVertical = Input.GetAxisRaw("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        if (movement.x != 0) {
            this.transform.Rotate(0, movement.x * 5f, 0);
        }
        if (movement.z != 0) {
            this.transform.position += transform.forward * moveSpeed;
        }
    }