//Calculate and return movement direction based on player input;
//This function can be overridden by inheriting scripts to implement different player controls;
protected virtual Vector3 CalculateMovementDirection()
{
//If no character input script is attached to this object, return;
if (characterInput == null)
{
return(Vector3.zero);
}
Vector3 _velocity = Vector3.zero;
//If no camera transform has been assigned, use the character's transform axes to calculate the movement direction;
if (cameraTransform == null)
{
_velocity += tr.right * characterInput.GetHorizontalMovementInput();
_velocity += tr.forward * characterInput.GetVerticalMovementInput();
}
else
{
//If a camera transform has been assigned, use the assigned transform's axes for movement direction;
//Project movement direction so movement stays parallel to the ground;
_velocity += Vector3.ProjectOnPlane(cameraTransform.right, tr.up).normalized *characterInput.GetHorizontalMovementInput();
_velocity += Vector3.ProjectOnPlane(cameraTransform.forward, tr.up).normalized *characterInput.GetVerticalMovementInput();
}
//If necessary, clamp movement vector to magnitude of 1f;
if (_velocity.magnitude > 1f)
{
_velocity.Normalize();
}
return(_velocity);
}