Camera Relative Movement

I’m trying to write my own controller for a third person character and I’m having some trouble making the character move relative to a camera i.e. pressing up will always make the character walk away from the camera, pressing right will always cause the character to move right relative to the camera.

Here’s my current code for setting the rotation of the character:

private void updateRotation(){
		Transform cameraTransform = theCamera.transform;
		// forward of the camera on the x-z plane
		Vector3 cameraForward = theCamera.transform.TransformDirection(Vector3.forward);
		cameraForward.y = 0f;
		cameraForward = cameraForward.normalized;
		
		Vector3 cameraRight = new Vector3(cameraForward.z, 0.0f, -cameraForward.x);
		
		float vert = Input.GetAxisRaw("Vertical");
		float hor = Input.GetAxisRaw("Horizontal");
		
		Vector3 tempTarget = hor * cameraRight + vert * cameraForward;
		if (tempTarget!= Vector3.zero){
			theController.moveDirection = Vector3.RotateTowards(theController.moveDirection, tempTarget, theController.rotSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
			theController.moveDirection = theController.moveDirection.normalized;
			theController.transform.rotation = Quaternion.LookRotation(theController.moveDirection);
		}
		
	}

And here is how my controller moves the character forward:

private void moveCharacter(){
		transform.Translate(transform.forward*moveSpeed*Time.deltaTime);
}

Currently the character moves relative to the camera at first, then seems to “lose it’s axis” so to speak.
Any help would be appreciated.

I believe the root of the issue is that Transform.Translate take a Local not a World coordinate. The character movement code should be:

transform.Translate(Vector3.forward*moveSpeed*Time.deltaTime);

or you could do:

transform.position += transform.forward*moveSpeed*Time.deltaTime;

A few other comments:

  • Lines 4-6 can be replaced by ‘theCamera.transform.forward’ (which is normalized).
  • Your cameraRight code does not calculate the right of the camera…it is an angle right. If you want the actual right (which I think you do) you can just use ‘theCamera.transform.right’.
  • Why GetAxisRaw() instead of GetAxis()?

So you can reduce lines 1 - 12 to:

Vector3 tempTarget = Input.GetAxis("Vertical") * theCamera.transform.forward + Input.GetAxis("Horizontal) * theCamera.transform.right;

I know its old but I wrote one recently so I’ll write some tips while its fresh in my mind.

I wrote for Android, so a visual stick is present on screen.

I made animations for Walk/Run forward, backward and turn left, turn right.

Stick.Y = Walk

Stick.X = Turn.

So normally, up on stick makes character walk forward no matter his facing direction. This is your link between Camera and Joystick.
Up on stick and Forward on Camera correspond to 0 degrees.

The formula goes

Jr = -P + Jp

(Jr = Joystick Rotation Required, P = Player rotation from camera, Jp = Joystick Press Rotation)

So, find clockwise rotation of Player (P) with respect to camera forward. Make it Negative.
Use AngleAxis to create a rotation of this angle around Vector3.up.
Multiply the Quaternion by your input stick vector.

Note, mapping Stick x,y to Vector3 x,z helps with simplistic Vector2 rotation. Also one will require the use of Vector3.cross to find Angles over 180 degrees.