Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Vector3 forwardMovement;
- Vector3 rightMovement;
- Animator animator;
- [Header("Player Input")]
- [SerializeField] private string leftTrigger;
- [SerializeField] private string rightTrigger;
- private float leftTriggerValue;
- private float rightTriggerValue;
- [Header("Climbing System")]
- [Range(0, 1)] [SerializeField] float delta = .5f;
- [SerializeField] float bracedYOffset = 1.2f;
- [SerializeField] float hangingYOffset = 1.8f;
- RaycastHit hit;
- RaycastHit hitTouching;
- RaycastHit hitClimbMode;
- RaycastHit hitLedgeLeft;
- RaycastHit hitLedgeRight;
- RaycastHit hitLedgeDown;
- RaycastHit hitDown;
- [Header("Player status confirmation")]
- bool canLeftHand, canRightHand;
- public bool canClimb;
- public bool canBrace; //If he can't brace then he hangs
- [Header("Hand IK")]
- [SerializeField] Transform rightHandObj;
- [SerializeField] Transform leftHandObj;
- private float leftHandWeight;
- private float rightHandWeight;
- private void Awake()
- {
- animator = GetComponent<Animator>();
- }
- private void Update()
- {
- leftTriggerValue = Input.GetAxis(leftTrigger);
- rightTriggerValue = Input.GetAxis(rightTrigger);
- }
- private void FixedUpdate()
- {
- ClimbSystem();
- }
- private void ClimbSystem()
- {
- //Left Hand
- if (Physics.Raycast(transform.position + Vector3.up * 2f + transform.forward * .5f, transform.TransformDirection(new Vector3(-1f, -1.5f, 0)), out hitLedgeLeft, 1.5f)
- &&
- leftTriggerValue >= Mathf.Epsilon)
- {
- canLeftHand = true;
- }
- else
- {
- canLeftHand = false;
- }
- //Right Hand
- if (Physics.Raycast(transform.position + Vector3.up * 2f + transform.forward * .5f, transform.TransformDirection(new Vector3(1f, -1.5f, 0)), out hitLedgeRight, 1.5f)
- &&
- rightTriggerValue >= Mathf.Epsilon)
- {
- canRightHand = true;
- }
- else
- {
- canRightHand = false;
- }
- canClimb = (canLeftHand && canRightHand) ? true : false;
- if (canClimb)
- {
- leftHandWeight = Mathf.Lerp(leftHandWeight, 1f, delta);
- rightHandWeight = Mathf.Lerp(rightHandWeight, 1f, delta);
- leftHandObj.position = hitLedgeLeft.point;
- rightHandObj.position = hitLedgeRight.point;
- //Braced or Hanging
- //Braced
- if (Physics.Raycast(transform.position + Vector3.up * 0.5f, transform.TransformDirection(Vector3.forward), out hitClimbMode, .7f)) //Raycast in the belly
- {
- /*Vector3 newPos = new Vector3(hitLedgeDown.point.x, hitLedgeDown.point.y - bracedYOffset, hitLedgeDown.point.z);
- newPos = newPos - (transform.forward * .3f);
- transform.position = newPos;*/
- canBrace = true;
- }
- //Hanging
- else
- {
- /*Vector3 newPos = new Vector3(hitLedgeDown.point.x, hitLedgeDown.point.y - hangingYOffset, hitLedgeDown.point.z);
- newPos = newPos - (transform.forward * .3f);
- transform.position = newPos;*/
- canBrace = false;
- }
- }
- else
- {
- leftHandWeight = Mathf.Lerp(leftHandWeight, 0f, delta);
- rightHandWeight = Mathf.Lerp(rightHandWeight, 0f, delta);
- }
- //Raycast debugging
- //ClimbMode (Hang or Braced)
- Debug.DrawRay(transform.position + Vector3.up * 0.5f, transform.TransformDirection(Vector3.forward) * .7f, Color.red);
- //Checking for ledges
- Debug.DrawRay(transform.position + Vector3.up * 2f + transform.forward * .5f, transform.TransformDirection(new Vector3(1f, -1.5f, 0)) * 1.5f, Color.blue);
- Debug.DrawRay(transform.position + Vector3.up * 2f + transform.forward * .5f, transform.TransformDirection(new Vector3(-1f, -1.5f, 0)) * 1.5f, Color.blue);
- //Debug.DrawRay(transform.position + Vector3.up * 2f + transform.forward * .3f, transform.TransformDirection(new Vector3(1f, -3f, 0)) * .5f, Color.blue);
- //Debug.DrawRay(transform.position + Vector3.up * 2f + transform.forward * .3f, transform.TransformDirection(new Vector3(-1f, -3f, 0)) * .5f, Color.blue);
- Debug.DrawRay(transform.position + Vector3.up * 2f + transform.forward * .5f, Vector3.up * -1.5f, Color.blue);
- }
- private void OnAnimatorIK(int layerIndex)
- {
- //leftHandObj.position = hitLedgeLeft.point;
- if (rightHandObj != null && leftHandObj != null)
- {
- //Right Hand
- animator.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandWeight);
- animator.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandWeight);
- animator.SetIKPosition(AvatarIKGoal.RightHand, rightHandObj.position);
- animator.SetIKRotation(AvatarIKGoal.RightHand, rightHandObj.rotation);
- //Left Hand
- animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandWeight);
- animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandWeight);
- animator.SetIKPosition(AvatarIKGoal.LeftHand, leftHandObj.position);
- animator.SetIKRotation(AvatarIKGoal.LeftHand, leftHandObj.rotation);
- }
- else
- {
- animator.SetIKPositionWeight(AvatarIKGoal.RightHand, rightHandWeight);
- animator.SetIKRotationWeight(AvatarIKGoal.RightHand, rightHandWeight);
- animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftHandWeight);
- animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftHandWeight);
- animator.SetLookAtWeight(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement