Okay, so I think I’ve finally found a working script for 2D pathfinding. The only problem is that the enemy prefab I’ve attached it to is moving ridiculously too fast. Would appreciate any assistance on this one.
Here’s the script:
using UnityEngine;
using System.Collections;
public class Practice : MonoBehaviour {
public Transform[] wayPointPath1 = new Transform[6]; //an array to store each waypoint
int currentWayPoint = 0; //starting waypoint in the path
float rotationSpeed = 6.0f; //waypoint rotation speed
private float speed = 1f; //likely the speed variable to be deleted upon integration with EnemyControl script
void Start ()
{
//find each waypoint object in the scene when the game is loaded
wayPointPath1[0] = GameObject.Find("wp1").transform;
wayPointPath1[1] = GameObject.Find("wp2").transform;
wayPointPath1[2] = GameObject.Find("wp3").transform;
wayPointPath1[3] = GameObject.Find("wp4").transform;
wayPointPath1[4] = GameObject.Find("wp5").transform;
wayPointPath1[5] = GameObject.Find("wp6").transform;
}
void patrol()
{
Quaternion rotation = Quaternion.LookRotation(wayPointPath1[currentWayPoint].position - transform.position);
//control how fast enemy can rotate when face the next
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);
//movement
Vector3 wayPointDirection = wayPointPath1[currentWayPoint].position - transform.position;
float speedElement = Vector3.Dot(wayPointDirection.normalized, transform.forward);
float accelerate = speed * speedElement;
transform.Translate(transform.position.x, transform.position.y, Time.deltaTime * 0f);
}
}
Thanks again!
I actually did a little more searching after figuring out that the rotation aspect of my enemy movement was causing the biggest speed issues. Here’s the new code that I came up with after combining a few other scripts, until I found what I really needed. For anyone doing 2d waypoint creation from scratch and rotation along the z-axis, this might be for you. FYI: I commented out the OnDrawGizmos function, but if you need to see where you placed your waypoints, the code is there and you can uncomment it. Also, FYI: You’ll first want to create an empty GameObject (name it “Path” or something like that), then create empty children GameObjects and you can label them in order, as you please. Move the points wherever you want and then just drag them from the hierarchy into the slots on whatever GameObject you attach this script to.
public Transform path;
public float speed = 5.0f;
public float reachDist = 1.0f;
public int currentPoint = 0;
private float rotationSpeed = 6.0f;
// Use this for initialization
void Start () {
// I named each waypoint 'wp' + the corresponding number
path[0] = GameObject.Find("wp1").transform;
path[1] = GameObject.Find("wp2").transform;
path[2] = GameObject.Find("wp3").transform;
path[3] = GameObject.Find("wp4").transform;
path[4] = GameObject.Find("wp5").transform;
path[5] = GameObject.Find("wp6").transform;
}
// Update is called once per frame
void Update ()
{
EnemyMove();
}
void EnemyMove()
{
Quaternion rotation = Quaternion.LookRotation(path[currentPoint].position - transform.position, transform.TransformDirection(Vector3.forward));
transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
float dist = Vector3.Distance (path[currentPoint].position, transform.position);
transform.position = Vector3.MoveTowards (transform.position, path[currentPoint].position, Time.deltaTime * speed);
if(dist <= reachDist)
{
currentPoint++;
}
if(currentPoint >= path.Length)
{
Destroy(this.gameObject);
}
}
/*
void OnDrawGizmos ()
{
if(path.Length > 0)
{
for(int i = 0; i < path.Length; i++)
{
if(path *!= null)*
{
Gizmos.DrawSphere(path*.position, reachDist);*
}
}
}
} */
}
Hope this helps!
Firstly paste the code in the “code section” so the viewers can read it easier (its located above the texfield and is a button with numbers on.
And from what i can see you should change the speedElement float. Otherwise add an other float called speed that you use in the code INSTEAD of Time.deltaTime
And change it after your prefrence