Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerMovement : MonoBehaviour
- {
- public float turnSmoothing = 15f;
- public float speedDampTime = 0.1f;
- public float coverColliderRadius = 0.22f;
- public Transform handTarget;
- private Animator anim;
- private HashIDs hash;
- private bool canMove;
- private GameObject cameraParent;
- private CapsuleCollider col;
- void Awake()
- {
- anim = GetComponent<Animator>();
- hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
- cameraParent = GameObject.FindGameObjectWithTag(Tags.cameraParent);
- col = GetComponent<CapsuleCollider>();
- }
- void FixedUpdate()
- {
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- bool sneak = Input.GetButton("Sneak");
- bool roll = Input.GetButton("Roll");
- bool jump = Input.GetButton("Jump");
- MovementManagement(h, v, sneak, jump, roll);
- }
- void MovementManagement(float horizontal, float vertical, bool sneaking, bool jump, bool roll)
- {
- anim.SetBool(hash.sneakingBool, sneaking);
- anim.SetBool(hash.rollBoll, roll);
- anim.SetBool(hash.jumpBool, jump);
- if (anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.jumpState)
- {
- if (!anim.IsInTransition(0))
- {
- col.height = anim.GetFloat("ColliderHeight");
- anim.SetBool(hash.jumpBool, false);
- // Raycast down from the center of the character..
- Ray ray = new Ray(transform.position + Vector3.up, -Vector3.up);
- RaycastHit hitInfo = new RaycastHit();
- if (Physics.Raycast(ray, out hitInfo))
- {
- // ..if distance to the ground is more than 1.75, use Match Target
- if (hitInfo.distance > 1.75f)
- {
- // MatchTarget allows us to take over animation and smoothly transition our character towards a location - the hit point from the ray.
- // 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
- // of the timeline of our animation clip
- anim.MatchTarget(hitInfo.point, Quaternion.identity, AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(0, 1, 0), 0), 0.35f, 0.5f);
- }
- }
- }
- }
- if (anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.jumpUpState && !anim.IsInTransition(0))
- {
- anim.SetBool(hash.jumpBool, false);
- anim.SetBool(hash.nearLedgeBool, false);
- //collider.enabled = false;
- col.height = anim.GetFloat("ColliderHeight");
- col.center = new Vector3(col.center.x, anim.GetFloat(hash.colliderY), col.center.z);
- anim.MatchTarget(handTarget.position, handTarget.rotation, AvatarTarget.RightHand, new MatchTargetWeightMask(new Vector3(0, 1, 0), 0),
- anim.GetFloat(hash.jumpStart), anim.GetFloat(hash.jumpEnd));
- }
- else
- {
- col.center = new Vector3(col.center.x, 1, col.center.z);
- col.height = 2;
- }
- if (!SpecialStateAvailable())
- {
- anim.SetFloat(hash.speedFloat, 0);
- return; // return 'cuz we can't move while rolling
- }
- if (horizontal != 0f || vertical != 0f)
- {
- RotateTowardsMovement(horizontal, vertical);
- anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
- }
- else
- anim.SetFloat(hash.speedFloat, 0);
- }
- void RotateTowardsMovement(float horizontal, float vertical)
- {
- Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
- if (targetDirection != Vector3.zero)
- {
- targetDirection = cameraParent.transform.TransformDirection(targetDirection);
- Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
- Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
- rigidbody.MoveRotation(newRotation);
- }
- }
- bool SpecialStateAvailable()
- {
- return (anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locoState
- || anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.idleState
- || anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.sneakState);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement