-
-
Save DanielAppert/4d0260cc39a7f29a41da69f68fab0ed7 to your computer and use it in GitHub Desktop.
Simple 2D Waypoint Movement in Unity
This file contains 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
/** | |
* File: Mover.cs (1 of 2) | |
* | |
* SIMPLE 2D WAYPOINT MOVEMENT | |
* =========================== | |
* | |
* Original code by Abban Dunne, https://abandon.ie/ | |
* https://gist.github.com/Abban/42721b25cebba33c389a | |
* | |
* Modified by Daniel Appert, http://www.mountainpath.ch/ | |
* Adding Blend Tree support for prefabs | |
* | |
* USAGE | |
* ===== | |
* 1. Have a prefab containing a Blend Tree. The movement directions are given by DirectionX and DirectionY. | |
* 2. add the prefab to the scene and add the Mover.cs script. | |
* 2. create an empty game object for each waypoint you need. | |
* 3. add the Waypoint.cs script to each waypoint. | |
* 4. add the waypoints into the Waypoints array in the mover object (main object). | |
*/ | |
using UnityEngine; | |
using System.Collections.Generic; | |
using System.Collections; | |
public class Mover : MonoBehaviour | |
{ | |
public Waypoint[] wayPoints; | |
public float speed = 3f; | |
public bool isCircular; | |
// Always true at the beginning because the moving object will always move towards the first waypoint | |
public bool inReverse = true; | |
private Waypoint currentWaypoint; | |
private int currentIndex = 0; | |
private bool isWaiting = false; | |
private float speedStorage = 0; | |
//Get animator from prefab | |
Animator thisAnim; | |
/** | |
* Initialisation | |
* | |
*/ | |
void Start () { | |
if(wayPoints.Length > 0) { | |
currentWaypoint = wayPoints[0]; | |
} | |
//Get animator from prefab | |
thisAnim = GetComponent<Animator>(); | |
} | |
/** | |
* Update is called once per frame | |
* | |
*/ | |
void Update() | |
{ | |
if(currentWaypoint != null && !isWaiting) { | |
MoveTowardsWaypoint(); | |
} | |
} | |
/** | |
* Pause the mover | |
* | |
*/ | |
void Pause() | |
{ | |
isWaiting = !isWaiting; | |
} | |
/** | |
* Move the object towards the selected waypoint | |
* | |
*/ | |
private void MoveTowardsWaypoint() | |
{ | |
// Get the moving objects current position | |
Vector3 currentPosition = this.transform.position; | |
// Get the target waypoints position | |
Vector3 targetPosition = currentWaypoint.transform.position; | |
// If the moving object isn't that close to the waypoint | |
if(Vector3.Distance(currentPosition, targetPosition) > .1f) { | |
// Get the direction and normalize | |
Vector3 directionOfTravel = targetPosition - currentPosition; | |
directionOfTravel.Normalize(); | |
//scale the movement on each axis by the directionOfTravel vector components | |
this.transform.Translate( | |
directionOfTravel.x * speed * Time.deltaTime, | |
directionOfTravel.y * speed * Time.deltaTime, | |
directionOfTravel.z * speed * Time.deltaTime, | |
Space.World | |
); | |
//Get the correct animation from the Blend Tree | |
UpdateAnimation(directionOfTravel); | |
} else { | |
// If the waypoint has a pause amount then wait a bit | |
if(currentWaypoint.waitSeconds > 0) { | |
Pause(); | |
Invoke("Pause", currentWaypoint.waitSeconds); | |
} | |
// If the current waypoint has a speed change then change to it | |
if(currentWaypoint.speedOut > 0) { | |
speedStorage = speed; | |
speed = currentWaypoint.speedOut; | |
} else if(speedStorage != 0) { | |
speed = speedStorage; | |
speedStorage = 0; | |
} | |
NextWaypoint(); | |
} | |
} | |
/** | |
* Link the Blend Tree and this script. Get the correct animation. | |
* | |
*/ | |
void UpdateAnimation(Vector3 dir) | |
{ | |
thisAnim.SetFloat("DirectionX", dir.x); | |
thisAnim.SetFloat("DirectionY", dir.y); | |
} | |
/** | |
* Work out what the next waypoint is going to be | |
* | |
*/ | |
private void NextWaypoint() | |
{ | |
if(isCircular) { | |
if(!inReverse) { | |
currentIndex = (currentIndex+1 >= wayPoints.Length) ? 0 : currentIndex+1; | |
} else { | |
currentIndex = (currentIndex == 0) ? wayPoints.Length-1 : currentIndex-1; | |
} | |
} else { | |
// If at the start or the end then reverse | |
if((!inReverse && currentIndex+1 >= wayPoints.Length) || (inReverse && currentIndex == 0)) { | |
inReverse = !inReverse; | |
} | |
currentIndex = (!inReverse) ? currentIndex+1 : currentIndex-1; | |
} | |
currentWaypoint = wayPoints[currentIndex]; | |
} | |
} |
This file contains 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
/** | |
* File: Waypoint.cs (2 of 2) | |
* | |
* SIMPLE 2D WAYPOINT MOVEMENT | |
* =========================== | |
* | |
* Original code by Abban Dunne, https://abandon.ie/ | |
* https://gist.github.com/Abban/42721b25cebba33c389a | |
* | |
* Modified by Daniel Appert, http://www.mountainpath.ch/ | |
* Adding Blend Tree support for prefabs | |
* | |
* USAGE | |
* ===== | |
* 1. Have a prefab containing a Blend Tree. The movement directions are given by DirectionX and DirectionY. | |
* 2. add the prefab to the scene and add the Mover.cs script. | |
* 2. create an empty game object for each waypoint you need. | |
* 3. add the Waypoint.cs script to each waypoint. | |
* 4. add the waypoints into the Waypoints array in the mover object (main object). | |
*/ | |
using UnityEngine; | |
using System.Collections.Generic; | |
using System.Collections; | |
public class Waypoint : MonoBehaviour | |
{ | |
public float waitSeconds = 0; | |
public float speedOut = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment