Advertisement
Guest User

thirdPersonMotor.cs

a guest
Jan 4th, 2014
1,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.40 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class thirdPersonMotor : MonoBehaviour
  5. {
  6.     public static thirdPersonMotor instance;            // Reference to itself.
  7.  
  8.     // Player movespeeds.
  9.     public float forwardSpeed = 10.0f;
  10.     public float backwardSpeed = 6f;
  11.     public float strafingSpeed = 8f;
  12.     public float slidingSpeed = 10.0f;
  13.  
  14.     public float jumpSpeed = 5.5f;                      // Player jump speed.
  15.     public Vector3 moveVector { get; set; }    
  16.  
  17.     public float gravity = 9.81f;
  18.     public float terminalVelocity = 20f;
  19.     public float verticalVelocity { get; set; }
  20.  
  21.     public float slideThreshold = 0.7f;                 // Threshold that determines what kind of slopes will cause the player to slide.
  22.     public float maxControllableSlideMagnitude = 0.4f;  // Max slope at which the character can still be controlled.
  23.  
  24.     private Vector3 slideDirection;
  25.  
  26.     private void Awake ()
  27.     {
  28.         instance = this;
  29.     }
  30.  
  31.     public void UpdateMotor ()
  32.     {
  33.         ProcessMotion ();
  34.         SnapAlignCharacterWithCamera ();
  35.     }
  36.  
  37.     public void ProcessMotion()
  38.     {
  39.         // Transform moveVector to world space.
  40.         moveVector = transform.TransformDirection (moveVector);
  41.  
  42.         // Normalize moveVector if magnitude > 1.
  43.         if (moveVector.magnitude > 1)
  44.             moveVector = moveVector.normalized;
  45.  
  46.         // Apply sliding if requirements are met.
  47.         ApplySlide ();
  48.  
  49.         // Multiply moveVector by moveSpeed and deltaTime.
  50.         moveVector *= moveSpeed ();
  51.  
  52.         // Reapply vertical velocity to moveVector.y.
  53.         moveVector = new Vector3(moveVector.x, verticalVelocity, moveVector.z);
  54.  
  55.         // Apply Gravity
  56.         ApplyGravity ();
  57.  
  58.         // Move character in world space.
  59.         thirdPersonController.charController.Move (moveVector * Time.deltaTime);
  60.     }
  61.  
  62.     // If camera is moving, snap character to it.
  63.     public void SnapAlignCharacterWithCamera ()
  64.     {
  65.         if (moveVector.x != 0)
  66.         {
  67.             transform.rotation = Quaternion.Euler (transform.eulerAngles.x,
  68.                                                    Camera.main.transform.eulerAngles.y,
  69.                                                    transform.eulerAngles.z
  70.                                                    );
  71.         }
  72.     }
  73.  
  74.     private void ApplyGravity ()
  75.     {
  76.         // Apply gravity as long as the character is not in terminal velocity.
  77.         if (moveVector.y > -terminalVelocity)
  78.         {
  79.             moveVector = new Vector3(moveVector.x, moveVector.y - gravity * Time.deltaTime, moveVector.z);
  80.         }
  81.  
  82.         // If grounded, don't apply gravity.
  83.         if (thirdPersonController.charController.isGrounded && moveVector.y < 0)
  84.         {
  85.             moveVector = new Vector3(moveVector.x, 0, moveVector.z);
  86.         }
  87.     }
  88.  
  89.     private void ApplySlide ()
  90.     {
  91.         // Verify character is on the ground first.
  92.         if (!thirdPersonController.charController.isGrounded)
  93.         {
  94.             return;
  95.         }
  96.  
  97.         slideDirection = Vector3.zero;
  98.  
  99.         RaycastHit hitInfo;             // We use a raycast to determine the slope the character is on and store its data.
  100.  
  101.         if (Physics.Raycast(transform.position + Vector3.up, Vector3.down, out hitInfo))
  102.         {
  103.             if (hitInfo.normal.y < slideThreshold)
  104.             {
  105.                 slideDirection = new Vector3(hitInfo.normal.x, -hitInfo.normal.y, hitInfo.normal.z);
  106.             }
  107.         }
  108.  
  109.         // Check magnitude of the slope and handle normal control of character.
  110.         if (slideDirection.magnitude < maxControllableSlideMagnitude)
  111.         {
  112.             moveVector += slideDirection;
  113.         }
  114.         else
  115.         {
  116.             moveVector = slideDirection;
  117.         }
  118.     }
  119.  
  120.     public void Jump ()
  121.     {
  122.         if (thirdPersonController.charController.isGrounded)
  123.         {
  124.             verticalVelocity = jumpSpeed;
  125.         }
  126.     }
  127.  
  128.     private float moveSpeed ()
  129.     {
  130.         float moveSpeed = 0f;
  131.  
  132.         switch(thirdPersonAnimator.instance.moveDirection)
  133.         {
  134.         case thirdPersonAnimator.Direction.Stationary:
  135.             moveSpeed = 0f;
  136.             break;
  137.         case thirdPersonAnimator.Direction.Forward:
  138.             moveSpeed = forwardSpeed;
  139.             break;
  140.         case thirdPersonAnimator.Direction.Backward:
  141.             moveSpeed = backwardSpeed;
  142.             break;
  143.         case thirdPersonAnimator.Direction.LeftForward:
  144.             moveSpeed = forwardSpeed;
  145.             break;
  146.         case thirdPersonAnimator.Direction.RightForward:
  147.             moveSpeed = forwardSpeed;
  148.             break;
  149.         case thirdPersonAnimator.Direction.LeftBackward:
  150.             moveSpeed = backwardSpeed;
  151.             break;
  152.         case thirdPersonAnimator.Direction.RightBackward:
  153.             moveSpeed = backwardSpeed;
  154.             break;
  155.         case thirdPersonAnimator.Direction.Left:
  156.             moveSpeed = strafingSpeed;
  157.             break;
  158.         case thirdPersonAnimator.Direction.Right:
  159.             moveSpeed = strafingSpeed;
  160.             break;
  161.         }
  162.  
  163.         if (slideDirection.magnitude > 0)
  164.                         moveSpeed = slidingSpeed;
  165.  
  166.         return moveSpeed;
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement