Unity Tutorial: 3rd Person Vehicle, Steering with Mouse

Опубликовано: 13 Март 2026
на канале: Red Hen dev
947
22

A tutorial responding to James Hanvey's request -- how to steer a sphere using the mouse, with W for thrust, and with the camera following the sphere.

Apologies to James for almost immediately getting your name wrong: 'Harvey'. Sorry!

So, only two scripts needed. See below for the code :)

The steering setup will almost certainly need refactoring to deal with rotations messing up when pitch becomes too extreme: I will look for a clean solution!

Thanks for watching.

Code for the two scripts:

public class steerThrust : MonoBehaviour {

// Mouse direction (how much has mouse moved).
Vector2 mDir;

public float acc = 0.9f;

// Speed of vehicle.
float speed = 0f;

void Update () {
mouseSteer();
thrust ();
}

void thrust(){

// Pressed key?
// Increase speed.
if (Input.GetKey (KeyCode.W)) {
speed += acc;
}

// Translate by this transform's forward vector.
this.transform.Translate (this.transform.forward *
Time.deltaTime * speed);

// Deceleration/air friction.
speed *= 0.96f;
}

void mouseSteer(){
// What is the new mouse position on screen?
Vector2 mc = new Vector2(Input.GetAxisRaw("Mouse X"),
Input.GetAxisRaw("Mouse Y"));

// Add new movement to current mouse direction.
mDir += mc;

// Multiply both axes together and rotate this transform.
this.transform.localRotation =
Quaternion.AngleAxis (mDir.x, Vector3.up) *
Quaternion.AngleAxis (-mDir.y, Vector3.right);

}

}

***************
***************

public class CamFollow : MonoBehaviour {

// What shall we look at?
public Transform target;

void Update () {

// Look at target transform.
this.transform.LookAt (target.position);

// Set position to 10 units behind target.
this.transform.position = target.position +
(this.transform.forward * -10f);

}
}