- Home /
Question by
ericDEV · Jul 08, 2016 at 09:56 AM ·
c# tutorial
Problem with pathfinding tutorial
Hi, for anyone that answers thanks! Now i wathced this tutorial: https://www.youtube.com/watch?v=fvdRKS8x0aM and wrote this script. using UnityEngine; using System.Collections;
public class MonsterMotor : MonoBehaviour {
Transform target;
public float HP = 100;
public Transform[] path;
public float reacheDistance = 1;
public int curPoint = 0;
public float speed = 5;
float DeathTime = 3;
void Start () {
target = GameObject.FindGameObjectWithTag ("Player").transform;
}
// Update is called once per frame
void Update ()
{
Vector3 dir = path [curPoint].position - transform.position;
transform.position += dir * Time.deltaTime * speed;
if (dir.magnitude <= reacheDistance) {
curPoint++;
}
if (curPoint >= path.Length) {
curPoint = 0;
}
if (HP <= 0) {
Death ();
}
}
void Death ()
{
Destroy(gameObject, DeathTime);
}
void OnDrawGizmos ()
{
if (path.Length > 0) {
for (int i = 0; i < path.Length; i++) {
if (path [i] != null) {
Gizmos.DrawSphere (path [i].position, reacheDistance);
}
}
}
}
}
But the cube just stops at the first marker. Thanks again for anyone for answering.
Comment
Your answer