Advertisement
Guest User

ClimbingSystem

a guest
Mar 7th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.78 KB | None | 0 0
  1.     Vector3 forwardMovement;
  2.     Vector3 rightMovement;
  3.  
  4.     Animator animator;
  5.  
  6.     [Header("Player Input")]
  7.     [SerializeField] private string leftTrigger;
  8.     [SerializeField] private string rightTrigger;
  9.     private float leftTriggerValue;
  10.     private float rightTriggerValue;
  11.  
  12.     [Header("Climbing System")]
  13.     [Range(0, 1)] [SerializeField] float delta = .5f;
  14.     [SerializeField] float bracedYOffset = 1.2f;
  15.     [SerializeField] float hangingYOffset = 1.8f;
  16.     RaycastHit hit;
  17.     RaycastHit hitTouching;
  18.     RaycastHit hitClimbMode;
  19.     RaycastHit hitLedgeLeft;
  20.     RaycastHit hitLedgeRight;
  21.     RaycastHit hitLedgeDown;
  22.     RaycastHit hitDown;
  23.  
  24.     [Header("Player status confirmation")]
  25.     bool canLeftHand, canRightHand;
  26.     public bool canClimb;
  27.     public bool canBrace; //If he can't brace then he hangs
  28.  
  29.     [Header("Hand IK")]
  30.     [SerializeField] Transform rightHandObj;
  31.     [SerializeField] Transform leftHandObj;
  32.     private float leftHandWeight;
  33.     private float rightHandWeight;
  34.  
  35.     private void Awake()
  36.     {
  37.         animator = GetComponent<Animator>();
  38.     }
  39.  
  40.     private void Update()
  41.     {
  42.         leftTriggerValue = Input.GetAxis(leftTrigger);
  43.         rightTriggerValue = Input.GetAxis(rightTrigger);
  44.     }
  45.  
  46.     private void FixedUpdate()
  47.     {
  48.         ClimbSystem();
  49.     }
  50.  
  51.     private void ClimbSystem()
  52.     {
  53.         //Left Hand
  54.         if (Physics.Raycast(transform.position + Vector3.up * 2f + transform.forward * .5f, transform.TransformDirection(new Vector3(-1f, -1.5f, 0)), out hitLedgeLeft, 1.5f)
  55.             &&
  56.             leftTriggerValue >= Mathf.Epsilon)
  57.         {
  58.             canLeftHand = true;
  59.         }
  60.         else
  61.         {
  62.             canLeftHand = false;
  63.         }
  64.         //Right Hand
  65.         if (Physics.Raycast(transform.position + Vector3.up * 2f + transform.forward * .5f, transform.TransformDirection(new Vector3(1f, -1.5f, 0)), out hitLedgeRight, 1.5f)
  66.             &&
  67.             rightTriggerValue >= Mathf.Epsilon)
  68.         {
  69.             canRightHand = true;
  70.         }
  71.         else
  72.         {
  73.             canRightHand = false;
  74.         }
  75.  
  76.         canClimb = (canLeftHand && canRightHand) ? true : false;
  77.  
  78.         if (canClimb)
  79.         {
  80.             leftHandWeight = Mathf.Lerp(leftHandWeight, 1f, delta);
  81.             rightHandWeight = Mathf.Lerp(rightHandWeight, 1f, delta);
  82.             leftHandObj.position = hitLedgeLeft.point;
  83.             rightHandObj.position = hitLedgeRight.point;
  84.  
  85.             //Braced or Hanging
  86.             //Braced
  87.             if (Physics.Raycast(transform.position + Vector3.up * 0.5f, transform.TransformDirection(Vector3.forward), out hitClimbMode, .7f)) //Raycast in the belly
  88.             {
  89.                 /*Vector3 newPos = new Vector3(hitLedgeDown.point.x, hitLedgeDown.point.y - bracedYOffset, hitLedgeDown.point.z);
  90.                 newPos = newPos - (transform.forward * .3f);
  91.                 transform.position = newPos;*/
  92.                 canBrace = true;
  93.             }
  94.             //Hanging
  95.             else
  96.             {
  97.                 /*Vector3 newPos = new Vector3(hitLedgeDown.point.x, hitLedgeDown.point.y - hangingYOffset, hitLedgeDown.point.z);
  98.                 newPos = newPos - (transform.forward * .3f);
  99.                 transform.position = newPos;*/
  100.                 canBrace = false;
  101.             }
  102.  
  103.         }
  104.         else
  105.         {
  106.             leftHandWeight = Mathf.Lerp(leftHandWeight, 0f, delta);
  107.             rightHandWeight = Mathf.Lerp(rightHandWeight, 0f, delta);
  108.         }
  109.         //Raycast debugging
  110.         //ClimbMode (Hang or Braced)
  111.         Debug.DrawRay(transform.position + Vector3.up * 0.5f, transform.TransformDirection(Vector3.forward) * .7f, Color.red);
  112.         //Checking for ledges
  113.         Debug.DrawRay(transform.position + Vector3.up * 2f + transform.forward * .5f, transform.TransformDirection(new Vector3(1f, -1.5f, 0)) * 1.5f, Color.blue);
  114.         Debug.DrawRay(transform.position + Vector3.up * 2f + transform.forward * .5f, transform.TransformDirection(new Vector3(-1f, -1.5f, 0)) * 1.5f, Color.blue);
  115.         //Debug.DrawRay(transform.position + Vector3.up * 2f + transform.forward * .3f, transform.TransformDirection(new Vector3(1f, -3f, 0)) * .5f, Color.blue);
  116.         //Debug.DrawRay(transform.position + Vector3.up * 2f + transform.forward * .3f, transform.TransformDirection(new Vector3(-1f, -3f, 0)) * .5f, Color.blue);
  117.         Debug.DrawRay(transform.position + Vector3.up * 2f + transform.forward * .5f, Vector3.up * -1.5f, Color.blue);
  118.     }
  119.  
  120.     private void OnAnimatorIK(int layerIndex)
  121.     {
  122.         //leftHandObj.position = hitLedgeLeft.point;
  123.         if (rightHandObj != null && leftHandObj != null)
  124.         {
  125.             //Right Hand
  126.             animator.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandWeight);
  127.             animator.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandWeight);
  128.             animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position);
  129.             animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);
  130.             //Left Hand
  131.             animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandWeight);
  132.             animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandWeight);
  133.             animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandObj.position);
  134.             animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandObj.rotation);
  135.         }
  136.         else
  137.         {
  138.             animator.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandWeight);
  139.             animator.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandWeight);
  140.             animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandWeight);
  141.             animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandWeight);
  142.             animator.SetLookAtWeight(0);
  143.         }
  144.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement