Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Move : MonoBehaviour {
- public List<Vector2> path; // contains the path points
- public bool bounce = false; // false = 1, 2, 3, 1, 2, 3 ... / true = 1, 2, 3, 2, 1, 2, 3 ...
- public float speed = 1.0f; // speed unit/s per 1 second
- private int currentIndex = -1; // saves current start index / -1 = disabled
- private bool forward = true; // switch for bounce
- private Rigidbody2D rb;
- void Start() {
- rb = GetComponent<Rigidbody2D>();
- if(path.Count > 0) {
- transform.position = path[0];
- currentIndex = 0;
- }
- }
- void FixedUpdate() {
- if(path.Count > 0) {
- var startPos = path[currentIndex];
- var targetPos = path[nextStep()];
- }
- }
- int nextStep() {
- int predictedIndex = currentIndex + ((forward)?1:-1);
- if(forward) {
- if(predictedIndex > path.Count) {
- if(bounce) {
- forward = !forward;
- predictedIndex = currentIndex - 1;
- } else {
- forward = true;
- predictedIndex = 0;
- }
- }
- } else {
- if(predictedIndex < 0) {
- forward = true;
- predictedIndex = currentIndex + 1;
- }
- }
- return(predictedIndex);
- }
- void OnDrawGizmosSelected() {
- Gizmos.color = new Color(1f, 1f, 0f, 0.05f);
- if(path.Count > 0) {
- Vector3 startPoint = new Vector3(path[0].x, path[0].y, 0f);
- for(int iterPath = 1;iterPath < path.Count;iterPath++) {
- Vector3 firstPoint = new Vector3(path[iterPath - 1].x, path[iterPath - 1].y, 0f);
- Vector3 secondPoint = new Vector3(path[iterPath].x, path[iterPath].y, 0f);
- if(iterPath == 1) {
- if(bounce) {
- gizArrow(secondPoint, firstPoint);
- } else {
- gizArrow(firstPoint, secondPoint);
- }
- } else if(iterPath == path.Count - 1) {
- gizArrow(firstPoint, secondPoint);
- if(!bounce) {
- gizArrow(secondPoint, startPoint);
- }
- } else {
- if(bounce) {
- Gizmos.DrawLine(firstPoint, secondPoint);
- } else {
- gizArrow(firstPoint, secondPoint);
- }
- }
- }
- }
- }
- public float arrowHeadLength = 5.25f;
- public float arrowHeadAngle = 15f;
- private void gizArrow(Vector3 start, Vector3 ziel) {
- Vector3 offsetTemp = new Vector3(ziel.x, ziel.y, 0f) - new Vector3(start.x, start.y, 0f);
- Vector3 offset = new Vector3(offsetTemp.x, offsetTemp.y);
- Quaternion normalizedVector = Quaternion.LookRotation(Vector3.Normalize(offset));
- Vector3 testPos1 = normalizedVector * ((Quaternion.Euler(0f, 90f, (180 - arrowHeadAngle)) * Vector3.left) * arrowHeadLength);
- Vector3 testPos2 = normalizedVector * ((Quaternion.Euler(0f, 90f, (180 - -arrowHeadAngle)) * Vector3.left) * arrowHeadLength);
- Vector3 testPos3 = normalizedVector * ((Quaternion.Euler(90f, 90f, (180 - arrowHeadAngle)) * Vector3.left) * arrowHeadLength);
- Vector3 testPos4 = normalizedVector * ((Quaternion.Euler(90f, 90f, (180 - -arrowHeadAngle)) * Vector3.left) * arrowHeadLength);
- Gizmos.DrawLine(start, ziel);
- Gizmos.DrawLine(ziel, ziel + testPos1);
- Gizmos.DrawLine(ziel, ziel + testPos2);
- Gizmos.DrawLine(ziel, ziel + testPos3);
- Gizmos.DrawLine(ziel, ziel + testPos4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement