Skip to content

Instantly share code, notes, and snippets.

@Voley
Created March 11, 2019 15:55
Show Gist options
  • Save Voley/e23a6668eb7cdce4dfb79ede66c913e7 to your computer and use it in GitHub Desktop.
Save Voley/e23a6668eb7cdce4dfb79ede66c913e7 to your computer and use it in GitHub Desktop.
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