/// <Summary> /// Calculates the physics states /// </Summary> private void UpdateCharacterPhysics() { // correct gravity this.tempRayInfo = GroundCheck(); // save the current groundCheck state if (!this.tempRayInfo.collided) // if not on the ground, add gravity { this.mainRB.AddForce(CharacterGravity, ForceMode.Impulse); // add gravity } else { CheckDeathState(); } // Stores the currenct velocity this.previousVelocity = this.mainRB.velocity.sqrMagnitude; }
// MOVEMENT - - - - - /// <summary> /// Evaluate the correct force to be added /// </summary> private Vector3 MovementForceCalculation(Vector3 moveDirection, RayOutInfo colInfo) { // calculate the correct movement force based on the ground slope //SLOPE - - - - - // calculate the direction of motion based on slop normal // Calculate the move direction based on the surface normal // the velocity is manipulated by the inclination Vector3 calculateForce = Quaternion.FromToRotation(this.transform.up, colInfo.collisionNormal) * (moveDirection * Vector3.Dot(colInfo.collisionNormal, Vector3.up)); // Drift compensation calculateForce *= Mathf.Clamp((1f - Math.Abs(Vector3.Dot(calculateForce.normalized, mainRB.velocity.normalized))) * mainRB.mass * this.driftPrevention , 1f, mainRB.mass); // Debug force Debug.DrawLine(this.transform.position, this.transform.position + calculateForce, Color.cyan); // Return the calculate vector return(calculateForce); }