Advertisement
NeoGriever

Untitled

Nov 7th, 2020
2,523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Move : MonoBehaviour {
  6.   public List<Vector2> path; // contains the path points
  7.  
  8.   public bool bounce = false; // false = 1, 2, 3, 1, 2, 3 ... / true = 1, 2, 3, 2, 1, 2, 3 ...
  9.  
  10.   public float speed = 1.0f; // speed unit/s per 1 second
  11.  
  12.   private int currentIndex = -1; // saves current start index / -1 = disabled
  13.   private bool forward = true; // switch for bounce
  14.  
  15.   private Rigidbody2D rb;
  16.   void Start() {
  17.     rb = GetComponent<Rigidbody2D>();
  18.     if(path.Count > 0) {
  19.       transform.position = path[0];
  20.       currentIndex = 0;
  21.     }
  22.   }
  23.   void FixedUpdate() {
  24.     if(path.Count > 0) {
  25.       var startPos = path[currentIndex];
  26.       var targetPos = path[nextStep()];
  27.  
  28.      
  29.     }
  30.   }
  31.   int nextStep() {
  32.     int predictedIndex = currentIndex + ((forward)?1:-1);
  33.     if(forward) {
  34.       if(predictedIndex > path.Count) {
  35.         if(bounce) {
  36.           forward = !forward;
  37.           predictedIndex = currentIndex - 1;
  38.         } else {
  39.           forward = true;
  40.           predictedIndex = 0;
  41.         }
  42.       }
  43.     } else {
  44.       if(predictedIndex < 0) {
  45.         forward = true;
  46.         predictedIndex = currentIndex + 1;
  47.       }
  48.     }
  49.     return(predictedIndex);
  50.   }
  51.   void OnDrawGizmosSelected() {
  52.     Gizmos.color = new Color(1f, 1f, 0f, 0.05f);
  53.     if(path.Count > 0) {
  54.       Vector3 startPoint = new Vector3(path[0].x, path[0].y, 0f);
  55.       for(int iterPath = 1;iterPath < path.Count;iterPath++) {
  56.         Vector3 firstPoint = new Vector3(path[iterPath - 1].x, path[iterPath - 1].y, 0f);
  57.         Vector3 secondPoint = new Vector3(path[iterPath].x, path[iterPath].y, 0f);
  58.  
  59.         if(iterPath == 1) {
  60.           if(bounce) {
  61.             gizArrow(secondPoint, firstPoint);
  62.           } else {
  63.             gizArrow(firstPoint, secondPoint);
  64.           }
  65.         } else if(iterPath == path.Count - 1) {
  66.           gizArrow(firstPoint, secondPoint);
  67.           if(!bounce) {
  68.             gizArrow(secondPoint, startPoint);
  69.           }
  70.         } else {
  71.           if(bounce) {
  72.             Gizmos.DrawLine(firstPoint, secondPoint);
  73.           } else {
  74.             gizArrow(firstPoint, secondPoint);
  75.           }
  76.         }
  77.       }
  78.     }
  79.   }
  80.   public float arrowHeadLength = 5.25f;
  81.   public float arrowHeadAngle = 15f;
  82.   private void gizArrow(Vector3 start, Vector3 ziel) {
  83.     Vector3 offsetTemp = new Vector3(ziel.x, ziel.y, 0f) - new Vector3(start.x, start.y, 0f);
  84.     Vector3 offset = new Vector3(offsetTemp.x, offsetTemp.y);
  85.     Quaternion normalizedVector = Quaternion.LookRotation(Vector3.Normalize(offset));
  86.     Vector3 testPos1 = normalizedVector * ((Quaternion.Euler(0f, 90f, (180 -  arrowHeadAngle)) * Vector3.left) * arrowHeadLength);
  87.     Vector3 testPos2 = normalizedVector * ((Quaternion.Euler(0f, 90f, (180 - -arrowHeadAngle)) * Vector3.left) * arrowHeadLength);
  88.     Vector3 testPos3 = normalizedVector * ((Quaternion.Euler(90f, 90f, (180 -  arrowHeadAngle)) * Vector3.left) * arrowHeadLength);
  89.     Vector3 testPos4 = normalizedVector * ((Quaternion.Euler(90f, 90f, (180 - -arrowHeadAngle)) * Vector3.left) * arrowHeadLength);
  90.     Gizmos.DrawLine(start, ziel);
  91.     Gizmos.DrawLine(ziel, ziel + testPos1);
  92.     Gizmos.DrawLine(ziel, ziel + testPos2);
  93.     Gizmos.DrawLine(ziel, ziel + testPos3);
  94.     Gizmos.DrawLine(ziel, ziel + testPos4);
  95.   }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement