Input X/Y relative to camera?

I’m using Eric’s FPSWalker Enhanced script. The script works great, but I notice that the X and Y values aren’t calculated relative to the camera. Is there a way to change this by using the “relativeTo” clause, or some other way?

The variables are called like this:

function FixedUpdate() {
var inputX = Input.GetAxis(“Horizontal”);
var inputY = Input.GetAxis(“Vertical”);

And then used like this:

	// If air control is allowed, check movement but don't touch the y component
	if (airControl && playerControl) {
		moveDirection.x = inputX * inputModifyFactor * runSpeed;
		moveDirection.z = inputY * inputModifyFactor * runSpeed;
	}

Any ideas greatly appreciated! Thank you!

1 Answer

1

Assuming ‘inputModifyFactor’ and ‘runSpead’ are floats, then you can move relative to the camera using this calculation:

moveDirection = inputX * inputModifyFactor * runSpeed * Camera.mainCamera.transform.forward;
moveDirection += inputY * inputModifyFactor * runSpeed * Camera.mainCamera.transform.right;

Depending on the orientation of the camera with respect to the player, you may have to use ‘-=’ instead of ‘+=’.

I don’t see all your code here, so I’m unsure if this code will be moving the character according to the world coordinates or according to the local coordinates of the character. If the latter, then you want to remove the ‘transform.TransformDirection()’ call on ‘moveDirection’.

Thank you!!