How do you tweak an object's velocity based on it's direction?

I am designing a prototype for a game in a 2D overhead view, I want to know when I turn the car, how would I set the velocity of the car in the direction it is facing. So far, I have made the car turn left and right, but I have only made it go forward and backwards on the X axis. Here is my code so far:

(Sorry if it is hard to read, I couldn’t figure out any other way to insert it into the box)


public class controlCar : MonoBehaviour {

float x;

void Update () {

if (Input.GetKey(‘w’)) {

this.rigidbody2D.velocity = new Vector2(0.5f, 0f);

}

if (Input.GetKey(‘s’)) {

this.rigidbody2D.velocity = new Vector2(-0.5f, 0f);

}

if (Input.GetKey(‘a’)) {

x++;
transform.rotation = Quaternion.Euler(0, 0, x);

}

if (Input.GetKey(‘d’)) {

x–;
transform.rotation = Quaternion.Euler(0, 0, x);

}

}


Thank you!

use the dot product between main direction and current velocity. dot will return a value between 0 and 1. 1 being fully aligned.