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 JustMove : MonoBehaviour {
- public List<Vector2> points = new List<Vector2>();
- [Range(0.0001f, 4.0000f)]
- public float speedPerSecond = 0.3f;
- public bool startAtPoint0 = true;
- public bool autostart = false;
- public bool loop = true;
- private bool running = false;
- private Rigidbody2D rb;
- public void Begin() {
- running = true;
- }
- void Start() {
- rb = GetComponent<Rigidbody2D>();
- if(startAtPoint0) {
- transform.position = new Vector3(points[0].x, points[0].y, transform.position.z);
- }
- if(autostart) {
- Begin();
- }
- }
- void FixedUpdate() {
- if(running && points.Count > 0) {
- Vector2 p = points[0];
- if(Vector2.Distance(p,new Vector2(transform.position.x, transform.position.y)) < speedPerSecond * Time.fixedDeltaTime) {
- points.RemoveAt(0);
- if(loop) {
- points.Add(p);
- }
- p = points[0];
- }
- Vector2 targetDirection = (p - new Vector2(transform.position.x, transform.position.y)).normalized;
- rb.velocity = targetDirection * speedPerSecond;
- }
- }
- void OnDrawGizmos() {
- Gizmos.color = Color.yellow;
- Gizmos.DrawLine(transform.position, points[0]);
- for(int i = 1;i < points.Count;i++) {
- Gizmos.DrawLine(points[i-1], points[i]);
- }
- Gizmos.DrawLine(points[points.Count - 1], points[0]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement