Advertisement
ZeronSix

assety

Jul 14th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.65 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     public float turnSmoothing = 15f;
  8.     public float speedDampTime = 0.1f;
  9.     public float coverColliderRadius = 0.22f;
  10.     public Transform handTarget;
  11.  
  12.     private Animator anim;
  13.     private HashIDs hash;
  14.     private bool canMove;
  15.     private GameObject cameraParent;
  16.     private CapsuleCollider col;
  17.  
  18.     void Awake()
  19.     {
  20.         anim = GetComponent<Animator>();
  21.         hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
  22.         cameraParent = GameObject.FindGameObjectWithTag(Tags.cameraParent);
  23.         col = GetComponent<CapsuleCollider>();
  24.     }
  25.  
  26.     void FixedUpdate()
  27.     {
  28.         float h = Input.GetAxis("Horizontal");
  29.         float v = Input.GetAxis("Vertical");
  30.         bool sneak = Input.GetButton("Sneak");
  31.         bool roll = Input.GetButton("Roll");
  32.         bool jump = Input.GetButton("Jump");
  33.  
  34.         MovementManagement(h, v, sneak, jump, roll);
  35.     }
  36.  
  37.     void MovementManagement(float horizontal, float vertical, bool sneaking, bool jump, bool roll)
  38.     {
  39.         anim.SetBool(hash.sneakingBool, sneaking);
  40.         anim.SetBool(hash.rollBoll, roll);
  41.         anim.SetBool(hash.jumpBool, jump);
  42.  
  43.         if (anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.jumpState)
  44.         {
  45.             if (!anim.IsInTransition(0))
  46.             {
  47.                 col.height = anim.GetFloat("ColliderHeight");
  48.  
  49.                 anim.SetBool(hash.jumpBool, false);
  50.  
  51.                 // Raycast down from the center of the character..
  52.                 Ray ray = new Ray(transform.position + Vector3.up, -Vector3.up);
  53.                 RaycastHit hitInfo = new RaycastHit();
  54.  
  55.                 if (Physics.Raycast(ray, out hitInfo))
  56.                 {
  57.                     // ..if distance to the ground is more than 1.75, use Match Target
  58.                     if (hitInfo.distance > 1.75f)
  59.                     {
  60.  
  61.                         // MatchTarget allows us to take over animation and smoothly transition our character towards a location - the hit point from the ray.
  62.                         // Here we're telling the Root of the character to only be influenced on the Y axis (MatchTargetWeightMask) and only occur between 0.35 and 0.5
  63.                         // of the timeline of our animation clip
  64.                         anim.MatchTarget(hitInfo.point, Quaternion.identity, AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(0, 1, 0), 0), 0.35f, 0.5f);
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.  
  70.         if (anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.jumpUpState && !anim.IsInTransition(0))
  71.         {
  72.             anim.SetBool(hash.jumpBool, false);
  73.             anim.SetBool(hash.nearLedgeBool, false);
  74.             //collider.enabled = false;
  75.             col.height = anim.GetFloat("ColliderHeight");
  76.             col.center = new Vector3(col.center.x, anim.GetFloat(hash.colliderY), col.center.z);
  77.             anim.MatchTarget(handTarget.position, handTarget.rotation, AvatarTarget.RightHand, new MatchTargetWeightMask(new Vector3(0, 1, 0), 0),
  78.                 anim.GetFloat(hash.jumpStart), anim.GetFloat(hash.jumpEnd));
  79.         }
  80.         else
  81.         {
  82.             col.center = new Vector3(col.center.x, 1, col.center.z);
  83.             col.height = 2;
  84.         }
  85.  
  86.         if (!SpecialStateAvailable())
  87.         {
  88.             anim.SetFloat(hash.speedFloat, 0);
  89.             return; // return 'cuz we can't move while rolling
  90.         }
  91.  
  92.         if (horizontal != 0f || vertical != 0f)
  93.         {
  94.             RotateTowardsMovement(horizontal, vertical);
  95.             anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
  96.         }
  97.         else
  98.             anim.SetFloat(hash.speedFloat, 0);
  99.     }
  100.  
  101.     void RotateTowardsMovement(float horizontal, float vertical)
  102.     {
  103.         Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
  104.         if (targetDirection != Vector3.zero)
  105.         {
  106.             targetDirection = cameraParent.transform.TransformDirection(targetDirection);
  107.             Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
  108.             Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
  109.             rigidbody.MoveRotation(newRotation);
  110.         }
  111.     }
  112.  
  113.     bool SpecialStateAvailable()
  114.     {
  115.         return (anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locoState
  116.                 || anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.idleState
  117.                 || anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.sneakState);
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement