I’m currently converting the unity 3d 3rd person controller starter assets into my own game, and I’ve made a lot of changes to the code but for the most part (vast exaggeration) its been going smoothly. However, I’ve run into this problem where for some reason whenever the camera is looking up or down, the character moves slowly, but when it looks straight, the character moves faster. It also affects jump height the same way, where if you look up or down you have lower jumpheight than if you were looking straight
I’ve narrowed it down to this line of code
Vector3 inputDirection = new Vector3(_input.moveInput.x, 0.0f, _input.moveInput.y).normalized;
inputDirection = inputDirection.x * cameraTransform.right.normalized + inputDirection.z * cameraTransform.forward.normalized;
Specifically the inputdirection.z * cameratransform.forward.normalized, because whenever I take that out the character doesnt change speed when the camera moves up or down, but they can only move left or right.
Here’s the rest of the code if you’re interested:
private void Move()
{
// set target speed based on move speed, sprint speed and if sprint is pressed
float targetSpeed = playerStats.currentSpeed;
// if there is no input, set the target speed to 0
if (_input.moveInput == Vector2.zero)
targetSpeed = 0.0f;
// a reference to the players current horizontal velocity
float currentHorizontalSpeed = new Vector3(_controller.velocity.x, 0.0f, _controller.velocity.z).magnitude;
float speedOffset = 0.1f;
float inputMagnitude = _input.analogMovement ? _input.moveInput.magnitude : 1f;
// accelerate or decelerate to target speed
if (currentHorizontalSpeed < targetSpeed - speedOffset || currentHorizontalSpeed > targetSpeed + speedOffset)
{
// creates curved result rather than a linear one giving a more organic speed change
// note T in Lerp is clamped, so we don't need to clamp our speed
_speed = Mathf.Lerp(currentHorizontalSpeed, targetSpeed * inputMagnitude, Time.deltaTime * SpeedChangeRate);
// round speed to 3 decimal places
_speed = Mathf.Round(_speed * 1000f) / 1000f;
}
else
{
_speed = targetSpeed;
}
// normalise input direction
Vector3 inputDirection = new Vector3(_input.moveInput.x, 0.0f, _input.moveInput.y).normalized;
inputDirection = inputDirection.x * cameraTransform.right.normalized + inputDirection.z * cameraTransform.forward.normalized;
Quaternion targetRotation = Quaternion.Euler(0, cameraTransform.eulerAngles.y, 0);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, 5 * Time.deltaTime);
// move the player
_controller.Move(inputDirection.normalized * (_speed * Time.deltaTime) + new Vector3(0.0f, _verticalVelocity, 0.0f) * Time.deltaTime); //Cinemago?
}
And here’s the _input.moveInput:
public void OnMove(InputValue value)
{
MoveInput(value.Get<Vector2>());
}
public void MoveInput(Vector2 newMoveDirection)
{
moveInput = newMoveDirection;
}
And lastly here’s a video:
If anyone could help with this I would really appreciate that. Thanks in advance!