Character Movement rotates Cinemachine Camera wildly
Hello,
I hope this finds the reader doing well out there any insight is appreciated.
I am trying to reverse engineer the 'starter assets' third person controller and am encountering a bit of an issue with rotations and walking backwards once I begin to adjust the prefab setup.
Issue
Whenever the character walks backward or receives backwards input - the camera and the character are rotated constantly in lockstep. The intended behaviour is that the character walks towards the camera (which remains facing it's original direction).
I've encountered this issue with the starter assets controller before and just kinda moved on but I'd really like to understand the behaviour that's causing this 'bug'.
I'm including the move and camera rotate functions below for context.
Goal
Ideally I'd like to have the entire player setup in one prefab (there are multiple players). This works fine on the standard assets controller up until the point I begin to 'fork' it to make changes. Generally this is when things fall apart for me and I get this bug where the character and camera no longer rotate independently.
After much time wasting I have to ask for help. I've included my 'adjusted' functions below that pertain to this functionality / 'bug' i've created.
Camera Rotation Function
public void CameraRotation(Vector2 lookValue) {
// if there is an input and camera position is not fixed
if (lookValue.sqrMagnitude >= _threshold && !LockCameraPosition) {
_cinemachineTargetYaw += lookValue.x * Time.deltaTime;
_cinemachineTargetPitch += lookValue.y * Time.deltaTime;
}
// clamp our rotations so our values are limited 360 degrees
_cinemachineTargetYaw = ClampAngle(_cinemachineTargetYaw, float.MinValue, float.MaxValue);
_cinemachineTargetPitch = ClampAngle(_cinemachineTargetPitch, BottomClamp, TopClamp);
// Cinemachine will follow this target
playerCinemachineFollowTarget.transform.rotation = Quaternion.Euler(_cinemachineTargetPitch + CameraAngleOverride, _cinemachineTargetYaw, 0.0f);
}
Character Movement Function
private void Move() {
// set target speed based on move speed, sprint speed and if sprint is pressed
float targetSpeed = _playerInputBridge.sprint ? SprintSpeed : MoveSpeed;
// a simplistic acceleration and deceleration designed to be easy to remove, replace, or iterate upon
// note: Vector2's == operator uses approximation so is not floating point error prone, and is cheaper than magnitude
// if there is no input, set the target speed to 0
if (_playerInputBridge.move == 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 = _playerInputBridge.analogMovement ? _playerInputBridge.move.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;
}
_animationBlend = Mathf.Lerp(_animationBlend, targetSpeed, Time.deltaTime * SpeedChangeRate);
// normalise input direction
Vector3 inputDirection = new Vector3(_playerInputBridge.move.x, 0.0f, _playerInputBridge.move.y).normalized;
// note: Vector2's != operator uses approximation so is not floating point error prone, and is cheaper than magnitude
// if there is a move input rotate player when the player is moving
if (_playerInputBridge.move != Vector2.zero) {
_targetRotation = Mathf.Atan2(inputDirection.x, inputDirection.z) * Mathf.Rad2Deg + _mainCamera.gameObject.transform.eulerAngles.y;
float rotation = Mathf.SmoothDampAngle(transform.eulerAngles.y, _targetRotation, ref _rotationVelocity, RotationSmoothTime);
// rotate to face input direction relative to camera position
transform.rotation = Quaternion.Euler(0.0f, rotation, 0.0f);
}
Vector3 targetDirection = Quaternion.Euler(0.0f, _targetRotation, 0.0f) * Vector3.forward;
// move the player
_controller.Move(targetDirection.normalized * (_speed * Time.deltaTime) + new Vector3(0.0f, _verticalVelocity, 0.0f) * Time.deltaTime);
// update animator if using character
_playerAnimatorController.UpdateAnimatorMoveValues(_animationBlend, inputMagnitude);
/*UpdatePlayerMovementState();*/
}
Your answer
Follow this Question
Related Questions
is it possible to edit the Continuous Move provider (Action based) to add a jump feature? 0 Answers
Input System: How to declare interaction types to compare to callback context? 0 Answers
No FindObjectsOfType in CharacterController.cs ? 0 Answers
Rotate on key press to an end 0 Answers
How do I Lerp input floats? 1 Answer