//Update; void Update() { //Get controller velocity; Vector3 _velocity = controller.GetVelocity(); //Split up velocity; Vector3 _horizontalVelocity = VectorMath.RemoveDotVector(_velocity, tr.up); Vector3 _verticalVelocity = _velocity - _horizontalVelocity; //Smooth horizontal velocity for fluid animation; _horizontalVelocity = Vector3.Lerp(oldMovementVelocity, _horizontalVelocity, smoothingFactor); oldMovementVelocity = _horizontalVelocity; animator.SetFloat("VerticalSpeed", _verticalVelocity.magnitude * VectorMath.GetDotProduct(_verticalVelocity.normalized, tr.up)); animator.SetFloat("HorizontalSpeed", _horizontalVelocity.magnitude); //If animator is strafing, split up horizontal velocity; if (useStrafeAnimations) { Vector3 _localVelocity = animatorTransform.InverseTransformVector(_horizontalVelocity); animator.SetFloat("ForwardSpeed", _localVelocity.z); animator.SetFloat("StrafeSpeed", _localVelocity.x); } //Pass values to animator; animator.SetBool("IsGrounded", controller.IsGrounded()); animator.SetBool("IsStrafing", useStrafeAnimations); }
void LateUpdate() { //Get controller velocity; Vector3 _velocity = controller.GetVelocity(); //Project velocity onto a plane defined by the 'up' direction of the parent transform; _velocity = Vector3.ProjectOnPlane(_velocity, parentTransform.up); float _magnitudeThreshold = 0.001f; //If the velocity's magnitude is smaller than the threshold, return; if (_velocity.magnitude < _magnitudeThreshold) { return; } //Normalize velocity direction; _velocity.Normalize(); //Get current 'forward' vector; Vector3 _currentForward = tr.forward; //Calculate (signed) angle between velocity and forward direction; float _angleDifference = VectorMath.GetAngle(_currentForward, _velocity, parentTransform.up); //Calculate angle factor; float _factor = Mathf.InverseLerp(0f, fallOffAngle, Mathf.Abs(_angleDifference)); //Calculate this frame's step; float _step = Mathf.Sign(_angleDifference) * _factor * Time.deltaTime * turnSpeed; //Clamp step; if (_angleDifference < 0f && _step < _angleDifference) { _step = _angleDifference; } else if (_angleDifference > 0f && _step > _angleDifference) { _step = _angleDifference; } //Add step to current y angle; currentYRotation += _step; //Clamp y angle; if (currentYRotation > 360f) { currentYRotation -= 360f; } if (currentYRotation < -360f) { currentYRotation += 360f; } //Set transform rotation using Quaternion.Euler; tr.localRotation = Quaternion.Euler(0f, currentYRotation, 0f); }
//Update; void Update() { //Get controller velocity; Vector3 _velocity = controller.GetVelocity(); //Calculate horizontal velocity; Vector3 _horizontalVelocity = VectorMath.RemoveDotVector(_velocity, tr.up); FootStepUpdate(_horizontalVelocity.magnitude); }
protected override void HandleCameraRotation() { //Execute normal camera rotation code; base.HandleCameraRotation(); if (turnCameraTowardMovementDirection && controller != null) { //Get controller velocity; Vector3 _controllerVelocity = controller.GetVelocity(); RotateTowardsVelocity(_controllerVelocity, cameraTurnSpeed); //Save current position for next frame; lastPosition = m_trans.position; } }