Created
March 11, 2019 15:55
-
-
Save Voley/e23a6668eb7cdce4dfb79ede66c913e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Events; | |
public class Movement : MonoBehaviour | |
{ | |
public event MovementFinishedHandler MovementFinished; | |
public delegate void MovementFinishedHandler(); | |
[SerializeField] | |
private Transform target; | |
[SerializeField] | |
private float moveSpeed; | |
[SerializeField] | |
private float terminationDistance = 0.01f; | |
[SerializeField] | |
private bool shouldMoveImmediately = false; | |
private bool shouldMove = true; | |
private bool movementFinished = false; | |
public void StartMovement() | |
{ | |
shouldMove = true; | |
} | |
public void Update() | |
{ | |
if (!movementFinished && (shouldMove || shouldMoveImmediately)) | |
{ | |
if (Vector3.Distance(target.position, transform.position) > terminationDistance) | |
{ | |
transform.position = | |
Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * moveSpeed); | |
} | |
else | |
{ | |
movementFinished = true; | |
MovementFinished?.Invoke(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment