I’m a complete newbie and learning 2D game development by trying to make a top-down tank driving game.
Here’s my code to control the tank sprite.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankController : MonoBehaviour
{
// Rigidbody2D rb2D;
public float maxForwardSpeed = 2.0f;
public float maxRotationSpeed = 50.0f;
void Start ()
{
// rb2D = GetComponent<Rigidbody2D>();
}
void Update ()
{
float forwardSpeed = Input.GetAxis("Vertical") * maxForwardSpeed;
float rotationSpeed = Input.GetAxis("Horizontal") * maxRotationSpeed;
transform.Translate(transform.up * forwardSpeed * Time.deltaTime);
transform.Rotate(0, 0, -rotationSpeed * Time.deltaTime);
}
}
It compiles and runs, and the tank forward and back movement is fine but when I rotate the tank to say 45 degrees (Northeast), the actual direction of movement when I press forward is +90 degrees (East). Similarly if I rotate the tank to 90 degrees (East) the actual direction of movement is down (South).
What is going on to cause this behaviour?
I expected the transform.Translate(transform.up * forwardSpeed * Time.deltaTime)
statement to move the tank in the exact direction that the sprite is pointing.