C# Player Movement help?

Hey guys I’ve done a very basic player movement script where a capsule can go back and forth and rotate to turn around and stuff. What I’m trying to find help in is how do I make it so that the player movement moves around like the 3rd person controller? So that it will always walk on the Z axis of the object and just do like static turns at 90° and 45° if 2 buttons are pressed
So like if you press up - doesn’t turn at all, just moves forward
press right - will turn 90° and move forward
press up + left - will turn 45° and move forward.

Any links that’ll help me out or a tutorial or some kind of link will be greatly appreciated.

public class CharacterControl : MonoBehaviour 
	public float rightSpeed;
	public float forwardSpeed;
	public float turnSmoothing = 15f;
	private CharacterController playerController;

	void Start()
		playerController = GetComponent<CharacterController>();

	void Update()
		Vector3 forward = transform.TransformDirection(Vector3.forward);
		float fspeed = forwardSpeed * Input.GetAxis("Vertical");

		Vector3 right = transform.TransformDirection(Vector3.right);
		float rspeed = forwardSpeed * Input.GetAxis("Horizontal");

		playerController.SimpleMove(fspeed * forward);
		playerController.SimpleMove(rspeed * right);

	void Rotating (float horizontal, float vertical)
		Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);

		Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);

		Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);


It sounds like you want the player movement to driven by the camera’s facing. You also always want the player to face the direction they are going (no strafing)

I would use something like this to get the character to face their velocity

	public void LookTowardsVelocity(){ //Call this whenever you move
		Quaternion _oldRotation = _player.transform.rotation;  //save old rotation
		_player.transform.LookAt (_player.transform.position + _rigid.velocity); //Looks towards velocity
		_player.transform.rotation = Quaternion.Slerp (_oldRotation, _player.transform.rotation, Time.deltaTime * _lerpSpeed) //Lerps beteween old rot and new rot

This will have your character rotating up if they jump or something like that so my implementation has a few more qualifiers. I also only use it when the character is jumping, which is what the velocity limits are about.

protected virtual void LookTowardsVelocity(){ //called when the player jumps
		Vector3 _noYVel = new Vector3 (_player.Rigid.velocity.x, 0, _player.Rigid.velocity.z); 
		if(_noYVel.magnitude > 6){ //This prevents the player from spinning wildly in the air
			Vector3 _back = _player.transform.position + _noYVel; //It's called back because it's mostly for wall jumps
			_player.transform.LookAt (_back); //you'll note no lerping in this version. That's beacuse Snapping is important, I may put the lerping back in later

Then some simple movement relative to the camera

void Movement(){ //gets called on fixed update
		Vector3 _forwardVector = _camera.transform.forward * Input.GetAxisRaw ("Vertical"); //get their forward direction
		Vector3 _rightVector = _camera.transform.right * Input.GetAxisRaw ("Horizontal"); //get theor left and right direction
		_player.Rigid.velocity = (_forwardVector + _rightVector).normalized * _player.speed; // Normalize the 2 vectors, and multiply them by the speed you want
		//If you don't always have flat ground, you will probably want to project the _fullVelocity vector over the ground at some point
		//You can use Add force, or set the velocity. The first gives you accelleration, the latter gives you tight controls. I use a hybrid, but this example just sets velocity

FPS1.1 The Overview. Unity3d FPS Game Design Tutorial. - YouTube This was one of the things I went through a bit of when I was first doing movement systems.

I don’t have a link or tutorial, but let me see if I got what you want right:

You want your player to move in forward relative to the camera, when you press up, to move right relative to your camera is facing when you press right, with possibility to combine for a sort of right-forward sort of movement?

I’m not really in the mood to type up a script, but here is basically what you should do:

-Convert your input into a direction (i.e “up” = vector3.forward, “right” = vector3.right, “up”+“right” = vector3(0.5,0,0.5)) ← something like that, this is not a good way of doing that but you get the idea.

-that direction will be in world space, but you want it to be relative to your camera position, so, transform that direction to be relative to your camera. Look into Transform.TransformDirection and Transform.InverseTransformDirection, I’m too lazy to look up which one goes from world to local and which one is from local to world, I always forget.

-Now you have the direction you actually want to move your player in, so have your player, well… move in that direction.

-Of course, you’ll also want to rotate your player object to actually face the direction it’s moving in, but I’m not going into that here.

So yeah, basically you just need to make your movement direction relative to your camera rotation.