That’s not the best practice, it’s much better to use something like path-definition an follow path scripts.
There is very clear and well explained video tutorial Creating 2D Games in Unity 4.5 #4 - Moving Platforms - YouTube
The most valuable advantage of this method - You (or Your level designer) can do it easily in Unity editor without any scripting.
So I’ve add my scripts below. You can easily modify them and trigger events on reaching the key points of the path. Take look on MaxDistanceToGoal parameter in follow path script. The main algorithm to calculate distances between to points, and actually the answer to your question - is comparison of squared vector magnitudes:
float MaxDistanceToGoal = .1f;
...
var distanceSquared = (transform.position - _currentPoint.Current.position).magnitude;
if (distanceSquared < MaxDistanceToGoal * MaxDistanceToGoal) {
// do something here
YouHaveReachedTheTarget();
}
In addition to features that implemented in tutorial:
- Construction of path manually in Unity editor - no hard code.
- Following types: MovingTowards and Lerp.
- Visual interpretation of paths in Unity editor.
i implemented some more logics, such as:
- Two types of path: Circle and “Ping-pong”.
- Reversing scale on X-axis (we did a 2D-platformer, so it was enough for our purposes… nevertheless You can implement reversing for other axis).
- Destroying moving objects (or their parents) on stop (it’s suitable for example for static canon that is making shots in some restricted area).
- Countdown counter for iterations (-1 endless iteration).
Maybe it’s off-topic but I think that You should take a look deeply on this stuff.
Path definition script:
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
public class PathDefenition : MonoBehaviour {
public Transform[] Points;
public enum PathType {
PingPong,
Circle
}
public PathType Type = PathType.PingPong;
public IEnumerator<Transform> GetPathEnumerator()
{
if (Points == null || Points.Length < 1)
yield break;
var direction = 1;
var index = 0;
while (true) {
yield return Points[index];
if (Points.Length == 1)
continue;
if (Type == PathType.PingPong) {
if (index <= 0)
direction = 1;
else if (index >= Points.Length - 1)
direction = -1;
index = index + direction;
}
else if (Type == PathType.Circle)
if (index == Points.Length - 1)
index = 0;
else
index += 1;
}
}
public void OnDrawGizmos() {
if (Points == null || Points.Length < 2)
return;
for (var i = 1; i < Points.Length; i++)
Gizmos.DrawLine (Points [i - 1].position, Points *.position);*
-
if (Type == PathType.Circle)*
-
Gizmos.DrawLine (Points [0].position, Points [Points.Length - 1].position);*
- }*
}
Follow path script:
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
public class FollowPath : MonoBehaviour {
-
public enum FollowType {*
-
MoveTowards,*
-
Lerp*
-
}*
-
public FollowType Type = FollowType.MoveTowards;*
-
public PathDefenition Path;*
-
public float Speed = 1;*
-
public float MaxDistanceToGoal = .1f;*
-
public bool reversexScale;*
-
public bool AlwaysFromStart = false;*
-
public bool DestroyOnStop = false;*
-
public bool DestroyParentOnStop = false;*
-
public int Delay = 1;*
-
public int Repeat = -1;*
-
private bool stop = false;*
-
private bool temp_stop = false;*
-
private IEnumerator _currentPoint;*
-
private Vector3 old_position;*
-
public bool IsStop() {*
-
return stop;*
-
}*
-
public bool IsTempStop() {*
-
return temp_stop;*
-
}*
-
void MoveNextPointHandler() {*
-
_currentPoint.MoveNext ();*
-
if (*
-
(_currentPoint.Current.position.x - old_position.x > 0 && transform.localScale.x < 0 && reversexScale) ||*
-
(_currentPoint.Current.position.x - old_position.x < 0 && transform.localScale.x > 0 && reversexScale)*
-
) {*
_ transform.localScale = new Vector3 (transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);_
-
}*
-
old_position = _currentPoint.Current.position;*
-
}*
-
bool MoveNextPoint() {*
-
if (Repeat > 0) {*
-
Repeat -= 1;*
-
}*
-
if (Repeat >= 0) {*
-
if (Repeat == 0) {*
-
return false; *
-
}*
-
}*
-
MoveNextPointHandler();*
-
return true;*
-
}*
-
void Start () {*
-
if (Path == null) {*
-
Debug.LogError ("Path cannot be null", gameObject);*
-
return;*
-
}*
-
_currentPoint = Path.GetPathEnumerator ();*
-
_currentPoint.MoveNext ();*
-
old_position = _currentPoint.Current.position;*
-
if (_currentPoint.Current == null)*
-
return;*
-
transform.position = _currentPoint.Current.position;*
-
MoveNextPointHandler ();*
-
}*
-
void Update () {*
-
if (!stop) {*
-
if (!temp_stop) {*
-
if (_currentPoint == null || _currentPoint.Current == null)*
-
return;*
-
if (Type == FollowType.MoveTowards)*
transform.position = Vector3.MoveTowards (transform.position, _currentPoint.Current.position, Time.deltaTime * Speed);
transform.position = Vector3.Lerp (transform.position, _currentPoint.Current.position, Time.deltaTime * Speed);
_ if (distanceSquared < MaxDistanceToGoal * MaxDistanceToGoal) {_
-
if (AlwaysFromStart) {*
-
StartCoroutine (WaitForAnotherRunCoroutine ());*
-
} else {*
-
StartCoroutine (WaitForDelayCoroutine());*
-
}*
-
}*
-
}*
-
}*
-
else {*
-
if (transform.parent.gameObject && DestroyParentOnStop){*
-
Destroy(transform.parent.gameObject);*
-
} else if (DestroyOnStop){*
-
Destroy(transform.gameObject);*
-
}*
-
}*
-
}*
-
void stopAnimation() {*
-
if (gameObject.GetComponent<Animation>() != null) {*
-
gameObject.GetComponent<Animation>().Stop();*
-
}*
-
}*
-
IEnumerator WaitForAnotherRunCoroutine() {*
-
temp_stop = true;*
-
transform.GetComponent<Renderer>().enabled = false;*
-
MoveNextPointHandler();*
-
transform.position = _currentPoint.Current.position;*
-
stop = !MoveNextPoint ();*
-
if (Delay > 0) {*
-
stopAnimation();*
-
yield return new WaitForSeconds(Delay);*
-
stopAnimation();*
-
}*
-
transform.GetComponent<Renderer>().enabled = true;*
-
temp_stop = false;*
-
}*
-
IEnumerator WaitForDelayCoroutine() {*
-
temp_stop = true;*
-
stop = !MoveNextPoint ();*
-
if (Delay > 0) {*
-
yield return new WaitForSeconds(Delay);*
-
}*
-
if (!stop) {*
-
temp_stop = false;*
-
}*
-
yield break;*
-
}*
}
Hope that it will help. Have fun!