I fixed this problem, but use this as reference. I have added comments to where I changed the script.
using UnityEngine;
using System.Collections;
using Pathfinding;
public class AIPather : MonoBehaviour {
Seeker seeker;
Path path;
int currentWaypoint;
float speed = 20f;
CharacterController characterController;
float maxWaypointDistance = 2f;
public Vector3 target;
public GameObject moveMarker;
// Use this for initialization
void Start () {
seeker = GetComponent<Seeker>();
seeker.StartPath(transform.position, target, OnPathComplete);
characterController = GetComponent<CharacterController>();
}
public void OnPathComplete(Path p){
if (!p.error) {
path = p;
currentWaypoint = 0;
}
else {
Debug.LogError (p.error);
}
}
void FixedUpdate() {
if(Input.GetMouseButtonDown(1)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo)) {
Vector3 hitPoint = hitInfo.point;
//Instantiated wrong
Transform mark = Instantiate (moveMarker, new Vector3(hitPoint.x, 1, hitPoint.z), Quaternion.identity) as Transform;
seeker.GetNewPath(transform.position, mark.position);
}
}
if (path == null) {
return;
}
if (currentWaypoint >= path.vectorPath.Count) {
return;
}
Vector3 direction = (path.vectorPath[currentWaypoint]-transform.position).normalized * speed;
characterController.SimpleMove(direction);
if (Vector3.Distance (transform.position, path.vectorPath[currentWaypoint]) < maxWaypointDistance) {
currentWaypoint++;
}
}
void Update() {
/*if(Input.GetMouseButtonDown(1)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo)) {
Vector3 hitPoint = hitInfo.point;
Transform mark = Instantiate (moveMarker, new Vector3(hitPoint.x, 1, hitPoint.z), Quaternion.identity) as Transform;
seeker.StartPath(transform.position, mark.position);
}
}*/
}
}
I deleted seeker script because I fixed my previous problem.
Here is my new problem and script. My problem is that the character will not move after I set a new path.
using UnityEngine;
using System.Collections;
using Pathfinding;
public class AIPather : MonoBehaviour {
Seeker seeker;
Path path;
int currentWaypoint;
float speed = 20f;
CharacterController characterController;
float maxWaypointDistance = 2f;
public Vector3 target;
public GameObject moveMarker;
bool markerExists;
// Use this for initialization
void Start () {
seeker = GetComponent<Seeker>();
seeker.StartPath(transform.position, target, OnPathComplete);
characterController = GetComponent<CharacterController>();
}
public void OnPathComplete(Path p){
if (!p.error) {
path = p;
currentWaypoint = 0;
}
else {
Debug.LogError (p.error);
}
}
void FixedUpdate() {
if(Input.GetMouseButtonDown(1)) {
if(markerExists) {
GameObject prevMarker = GameObject.Find ("_moveMarkerCube(Clone)");
Destroy (prevMarker);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo)) {
Vector3 hitPoint = hitInfo.point;
Transform mark = Instantiate (moveMarker, new Vector3(hitPoint.x, 1, hitPoint.z), Quaternion.identity) as Transform;
seeker = GetComponent<Seeker>();
GameObject newMark = GameObject.Find ("_moveMarkerCube(Clone)");
seeker.StartPath(transform.position, newMark.transform.position, OnPathComplete);
markerExists = true;
}
}
else {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo)) {
Vector3 hitPoint = hitInfo.point;
Transform mark = Instantiate (moveMarker, new Vector3(hitPoint.x, 1, hitPoint.z), Quaternion.identity) as Transform;
seeker = GetComponent<Seeker>();
GameObject newMark = GameObject.Find ("_moveMarkerCube(Clone)");
seeker.GetNewPath(transform.position, newMark.transform.position);
markerExists = true;
}
}
}
if (path == null) {
return;
}
if (currentWaypoint >= path.vectorPath.Count) {
return;
}
Vector3 direction = (path.vectorPath[currentWaypoint]-transform.position).normalized * speed;
characterController.SimpleMove(direction);
if (Vector3.Distance (transform.position, path.vectorPath[currentWaypoint]) < maxWaypointDistance) {
currentWaypoint++;
}
}
void Update() {
/*if(Input.GetMouseButtonDown(1)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo)) {
Vector3 hitPoint = hitInfo.point;
Transform mark = Instantiate (moveMarker, new Vector3(hitPoint.x, 1, hitPoint.z), Quaternion.identity) as Transform;
seeker.StartPath(transform.position, mark.position);
}
}*/
}
}