Hi again playmakers
I have just fiddled around with the A* free pathfinding and have managed to get an AI moving to a ceartain pre-defined set of coordinates while generating a path and avoiding obstacles on the way
However - What do i do if i want this AI to be an enemy character that chases the player? are there any tools in playmaker where i can make an AI move while using the pathfinding data i have collected, or do i have to code the chasing alltogether?
This is what i have for the AI so far;
using UnityEngine;
using System.Collections;
using Pathfinding;
public class Movescript : MonoBehaviour {
public Vector3 targetPosition;
private Seeker seeker;
private CharacterController controller;
public Path path;
public float speed = 100;
public float nextWaypointDistance = 2;
private int currentWaypoint = 0;
public void Start () {
seeker = GetComponent<Seeker>();
controller = GetComponent<CharacterController>();
seeker.StartPath (transform.position,targetPosition, OnPathComplete);
}
public void OnPathComplete (Path p) {
if (!p.error) {
path = p;
currentWaypoint = 0;
}
}
public void FixedUpdate () {
if (path == null) {
return;
}
if (currentWaypoint >= path.vectorPath.Length) {
return;
}
Vector3 dir = (path.vectorPath[currentWaypoint]-transform.position).normalized;
dir *= speed * Time.fixedDeltaTime;
controller.SimpleMove (dir);
if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
currentWaypoint++;
return;
}
}
}
So basically what i understand of this is that it supplies the enemy AI with the target coordinates variable and it generates a path and moves along it to the target
but how would i go about making that variable a global var so i can change it every frame to update coords with the players position, and is that even possible?
in short, I want the enemy AI to chase the player and still avoid collisions, but what i have right now only works with a static coordinate that can't be changed
help please =(
--EDIT:
I now have the player saving his coordinates as a vector3 var and the pathfinder is now taking those coordinates as the travel destination - but again, it only does this once, and if i move it will just walk to the location where i started as a player and then stop there
I have tried changing the start function to an update function without any success
-sorry, im not the strongest in coding (which is why i'm here in the first place)