Advertisement
voodooplay

Untitled

Oct 14th, 2021
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class LegMover : MonoBehaviour
  6. {
  7.     public Transform legTarget;
  8.     public LayerMask groundLayer;
  9.     public float hoverDist;
  10.     public float groundCheckDistance;
  11.     public float legMoveDist;
  12.     public Vector3 halfWayPoint;
  13.     public float liftDistance;
  14.  
  15.     public float legMovementSpeed;
  16.     public int posIndex;
  17.     public Vector3 targetPoint;
  18.     Vector3 oldPos;
  19.     public bool grounded;
  20.     public LegMover opposingLeg;
  21.     public AudioSource aud;
  22.     // Start is called before the first frame update
  23.     void Start()
  24.     {
  25.        
  26.         aud = gameObject.GetComponent<AudioSource>();
  27.     }
  28.  
  29.     // Update is called once per frame
  30.     void Update()
  31.     {
  32.         CheckGround();
  33.  
  34.  
  35.  
  36.  
  37.         if (Vector2.Distance(transform.position, legTarget.position) > legMoveDist && posIndex == 0 && opposingLeg.grounded == true)
  38.         {
  39.             oldPos = legTarget.position;
  40.             targetPoint = transform.position;
  41.             halfWayPoint = (targetPoint + legTarget.position) / 2;
  42.             halfWayPoint.y += liftDistance;
  43.             posIndex = 1;
  44.  
  45.  
  46.  
  47.         }
  48.  
  49.         else if (posIndex == 1)
  50.         {
  51.  
  52.             legTarget.position = Vector3.Lerp(legTarget.position, halfWayPoint, legMovementSpeed * Time.deltaTime);
  53.  
  54.  
  55.  
  56.             if (Vector2.Distance(legTarget.position, halfWayPoint) <= 0.1f)
  57.             {
  58.                 posIndex = 2;
  59.             }
  60.         }
  61.  
  62.         else if (posIndex == 2)
  63.         {
  64.  
  65.             legTarget.position = Vector3.Lerp(legTarget.position, targetPoint, legMovementSpeed * Time.deltaTime);
  66.  
  67.  
  68.  
  69.             if (Vector2.Distance(legTarget.position, targetPoint) < 0.1f)
  70.             {
  71.                 aud.pitch = Random.Range(1.8f, 1.9f);
  72.                 aud.Play();
  73.                 posIndex = 0;
  74.             }
  75.         }
  76.  
  77.         if (posIndex == 0)
  78.         {
  79.             grounded = true;
  80.         }
  81.         else
  82.         {
  83.             grounded = false;
  84.         }
  85.     }
  86.  
  87.     public void CheckGround()
  88.     {
  89.         RaycastHit2D hit = Physics2D.Raycast(gameObject.transform.position, Vector3.down, groundCheckDistance, groundLayer);
  90.         if (hit.collider != null)
  91.         {
  92.  
  93.             Vector3 point = hit.point; // gets the position where the leg hit something
  94.             point.y += hoverDist;
  95.             transform.position = point;
  96.         }
  97.     }
  98.  
  99.  
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement