Advertisement
Guest User

Unity climbing vixez

a guest
Sep 21st, 2017
1,123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.54 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class IKSnap : MonoBehaviour
  7. {
  8.  
  9.     public bool useIK;
  10.  
  11.     public bool leftHandIK;
  12.     public bool rightHandIK;
  13.     public bool leftFootIK;
  14.     public bool rightFootIK;
  15.  
  16.     public Vector3 leftHandPos;
  17.     public Vector3 rightHandPos;
  18.  
  19.     public Vector3 leftFootPos;
  20.     public Vector3 rightFootPos;
  21.  
  22.     public Quaternion rightHandRot;
  23.     public Quaternion leftHandRot;
  24.  
  25.     public Quaternion rightFootRot;
  26.     public Quaternion leftFootRot;
  27.  
  28.     public Vector3 playerRot;
  29.  
  30.     private Animator anim;
  31.     private Rigidbody rigidbody;
  32.     private bool hasDoneMatch;
  33.  
  34.     public float leftHandWeight = 1f;
  35.     public float rightHandWeight = 1f;
  36.  
  37.     private Vector3 curLeftHandPos;
  38.     private Vector3 curRightHandPos;
  39.  
  40.     private bool climbMode;
  41.  
  42.     // Use this for initialization
  43.     void Start ()
  44.     {
  45.         anim = GetComponent<Animator>();
  46.         rigidbody = GetComponent<Rigidbody>();
  47.         useIK = true;
  48.     }
  49.    
  50.     void FixedUpdate ()
  51.     {
  52.         // Create 45 degree angles for the raycasts
  53.         Vector3 right45 = (transform.forward + transform.up + (transform.right / 2)).normalized;
  54.         Vector3 left45 = (transform.forward + transform.up + (-transform.right / 2)).normalized;
  55.         Vector3 right45Foot = (transform.forward + (transform.right)).normalized;
  56.         Vector3 left45Foot = (transform.forward + (-transform.right)).normalized;
  57.  
  58.         Debug.DrawRay(transform.position + new Vector3(0.0f, 1.0f, 0), left45, Color.cyan);
  59.         Debug.DrawRay(transform.position, right45Foot, Color.cyan);
  60.  
  61.  
  62.         // right mouse click to toggle climbing mode
  63.         if (Input.GetMouseButtonDown(1))
  64.         {
  65.             climbMode = !climbMode;
  66.  
  67.         }
  68.  
  69.         if (!climbMode)
  70.         {
  71.             leftHandIK = false;
  72.             rightHandIK = false;
  73.             leftFootIK = false;
  74.             rightFootIK = false;
  75.             anim.SetBool("ledge", false);
  76.             rigidbody.useGravity = true;
  77.             anim.SetFloat("HandWeight", 0);
  78.             hasDoneMatch = false;
  79.  
  80.             return;
  81.         }
  82.         RaycastHit LHit;
  83.         RaycastHit RHit;
  84.  
  85.         playerRot = transform.eulerAngles;
  86.  
  87.         Debug.DrawRay(transform.position + new Vector3(0.0f, 1.0f, 0.0f).normalized, right45, Color.green);
  88.         Debug.DrawRay(transform.position + new Vector3(0.0f, 1.0f, 0.0f).normalized, left45, Color.red);
  89.  
  90.  
  91.         LayerMask layerMask = 1 << LayerMask.NameToLayer("Ledge"); // only check for collisions with layerX
  92.  
  93.         //Raycast from the player to detect if it hits  with the Ledge prefab
  94.         if (Physics.Raycast(transform.position + new Vector3(0.0f, 1.0f, 0), left45, out LHit, 1.5f, layerMask))
  95.         {
  96.             if (LHit.transform.tag == "Ledge")
  97.             {
  98.                 leftHandIK = true;
  99.                 leftHandRot = Quaternion.Euler(-10, transform.eulerAngles.y, 0);
  100.  
  101.                 // Get the location of the green bar of the prefab, so we know where to put the player's hand
  102.                 if (Physics.Raycast(LHit.point, Vector3.up, out LHit, 1.5f))
  103.                 {
  104.                     leftHandPos = new Vector3(LHit.point.x, LHit.point.y, LHit.point.z - 0.05f);
  105.                 }
  106.  
  107.             }
  108.  
  109.  
  110.         }
  111.         else
  112.         {
  113.             leftHandIK = false;
  114.         }
  115.  
  116.         // Same as left hand
  117.         if (Physics.Raycast(transform.position + new Vector3(0.0f, 1.0f, 0), right45, out RHit, 1.5f, layerMask))
  118.         {
  119.             if (RHit.transform.tag == "Ledge")
  120.             {
  121.                 rightHandIK = true;
  122.                 rightHandRot = Quaternion.Euler(-10, transform.eulerAngles.y, 0);
  123.                 if (Physics.Raycast(RHit.point, Vector3.up, out RHit, 1.5f))
  124.                 {
  125.                     rightHandPos = new Vector3(RHit.point.x, RHit.point.y, RHit.point.z - 0.05f);
  126.                 }
  127.             }
  128.            
  129.         }
  130.         else
  131.         {
  132.             rightHandIK = false;
  133.         }
  134.  
  135.         anim.SetBool("ledge", leftHandIK && rightHandIK);
  136.  
  137.         // If there is a point for the left and right hand, look for a point for the feet
  138.         if (leftHandIK && rightHandIK)
  139.         {
  140.             rigidbody.useGravity = false;
  141.  
  142.             RaycastHit LFootHit;
  143.             RaycastHit RFootHit;
  144.             if (Physics.Raycast(transform.position, left45Foot, out LFootHit, 1.5f))
  145.             {
  146.                 if (LFootHit.transform.tag == "LedgeFootPoint")
  147.                 {
  148.                     leftFootPos = new Vector3(LFootHit.point.x, LFootHit.point.y + 0.5f, LFootHit.point.z - 0.15f);
  149.                     leftFootIK = true;
  150.                 }
  151.                 else
  152.                 {
  153.                     leftFootIK = false;
  154.                 }
  155.             }
  156.             else
  157.             {
  158.                 leftFootIK = false;
  159.             }
  160.  
  161.             if (Physics.Raycast(transform.position, right45Foot, out RFootHit, 1.5f))
  162.             {
  163.                 if (RFootHit.transform.tag == "LedgeFootPoint")
  164.                 {
  165.                     rightFootPos = new Vector3(RFootHit.point.x, RFootHit.point.y + 0.5f, RFootHit.point.z - 0.15f);
  166.                     rightFootIK = true;
  167.                 }
  168.                 else
  169.                 {
  170.                     rightFootIK = false;
  171.                 }
  172.             }
  173.             else
  174.             {
  175.                 rightFootIK = false;
  176.             }
  177.         }
  178.         else
  179.         {
  180.             leftFootIK = false;
  181.             rightFootIK = false;
  182.             rigidbody.useGravity = true;
  183.         }
  184.     }
  185.  
  186.     void Update()
  187.     {
  188.      
  189.     }
  190.  
  191.     void OnAnimatorIK()
  192.     {
  193.         if (useIK)
  194.         {
  195.             //if (Math.Abs(anim.GetFloat("HandWeight") - 1.0f) < 0.05f)
  196.             //{
  197.             //    return;
  198.             //}
  199.  
  200.             //Move the hands to the location over time in code
  201.  
  202.             anim.SetFloat("HandWeight", 1, 0.1f, Time.deltaTime * 0.12f);
  203.             anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, anim.GetFloat("HandWeight"));
  204.             anim.SetIKPositionWeight(AvatarIKGoal.RightHand, anim.GetFloat("HandWeight"));
  205.             anim.SetIKPositionWeight(AvatarIKGoal.LeftFoot, anim.GetFloat("HandWeight"));
  206.             anim.SetIKPositionWeight(AvatarIKGoal.RightFoot, anim.GetFloat("HandWeight"));
  207.             if (leftHandIK)
  208.             {
  209.                 anim.SetIKRotationWeight(AvatarIKGoal.LeftHand, 1f);
  210.                 anim.SetIKPosition(AvatarIKGoal.LeftHand, leftHandPos);
  211.                 anim.SetIKRotation(AvatarIKGoal.LeftHand, leftHandRot);
  212.             }
  213.  
  214.             if (rightHandIK)
  215.             {
  216.  
  217.                 anim.SetIKRotationWeight(AvatarIKGoal.RightHand, 1f);
  218.                 anim.SetIKPosition(AvatarIKGoal.RightHand, rightHandPos);
  219.                 anim.SetIKRotation(AvatarIKGoal.RightHand, rightHandRot);
  220.             }
  221.  
  222.             if (leftFootIK)
  223.             {
  224.                 anim.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 1f);
  225.                 anim.SetIKPosition(AvatarIKGoal.LeftFoot, leftFootPos);
  226.                 anim.SetIKRotation(AvatarIKGoal.LeftFoot, leftHandRot);
  227.             }
  228.  
  229.             if (rightFootIK)
  230.             {
  231.                 anim.SetIKRotationWeight(AvatarIKGoal.RightFoot, 1f);
  232.                 anim.SetIKPosition(AvatarIKGoal.RightFoot, rightFootPos);
  233.                 anim.SetIKRotation(AvatarIKGoal.RightFoot, rightHandRot);
  234.             }
  235.         }
  236.     }
  237.  
  238.     public bool OnLedge
  239.     {
  240.         get
  241.         {
  242.             if (leftHandIK && rightHandIK)
  243.             {
  244.                 return true;
  245.             }
  246.             return false;
  247.         }
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement