How do I change movement speed of my Raycasting A*pathfinding transform?

So I’m going for my first attempt at RayCasting with A*Pathfinding. Everything is going well I click and my path is set based on where the ray has hit, that’s all good. But my issue is setting the speed of my movement. Now I’m not using a character controller for various reasons.

Is there a simple way to slow the movement down? Here’s my code so far :slight_smile:

using UnityEngine;
using System.Collections;
using Pathfinding;

public class PlayerPather : MonoBehaviour {

	public GameObject theCamera;
	public RayCast cC;

	public Transform target;


	Seeker seeker;
	Path path;
	int currentWaypoint;

	float speed = 10;

	float maxWaypointDistance = 1f;

	// Use this for initialization
	void Start () {


		cC = theCamera.GetComponent<RayCast>();

		seeker = GetComponent<Seeker>();

		seeker.StartPath(transform.position, cC.targetposition, OnPathComplete);
	}
	


	public void OnPathComplete(Path p){
		if(!p.error)
		{
			path = p;
			currentWaypoint = 0;
			cC.gothere = false;
		}
		else {
			Debug.Log (p.error);
		}
	}

	void FixedUpdate(){
		if(cC.gothere == true){
		seeker.StartPath(transform.position, cC.targetposition, OnPathComplete);
		}
		if(path == null){
			return;
		}

		if(currentWaypoint >= path.vectorPath.Count){
			return;
		}

		transform.position = path.vectorPath[currentWaypoint];
		if(Vector3.Distance (transform.position, path.vectorPath[currentWaypoint]) < maxWaypointDistance){
		currentWaypoint++;
		
		}
	}
	

}

You are not using any speed at all, as far as I can see.
I expect that your character moves really fast to the target because you just put him on every waypoint.

You have to do something like this

void FixedUpdate () {
		if(path == null || pathComplete) {
			return;
		}
		
		Vector3 currTarget = path.vectorPath[currentWaypoint];

        Vector3 dir = (currTarget - transform.position).normalized;

        // Position
		transform.position += dir * speed * Time.deltaTime;
		
		if(Vector3.Distance(transform.position, currTarget) < nextWaypointDistance) {
			currentWaypoint++;
		}

		if(currentWaypoint >= path.vectorPath.Count) {
			pathComplete = true;
		}
	}

If you need some rotation, just let me know.

EDIT: Edited the code, I missed the direction Vector

Well, your script doesn’t even include any movement code, so that would help a lot :wink:

IEnumerator Move() {

		isMoving = true;


		Vector3 startPosition = transform.position;
		Vector3 endPosition = path.vectorPath[currentWaypoint];
		float distance = Vector3.Distance(startPosition, endPosition);

		float t = 0.0f;
		while(t < 1.0f) {
			t += Time.deltaTime / distance * moveSpeed;
			transform.position = Vector3.Lerp(startPosition, endPosition, t);
			yield return null;
		}

		isMoving = false;

	}

At the end of OnPathcomplete(), you add

doMove = true;

and in Update(), you add

if(doMove == true) {
			if(isMoving == false) {
				StartCoroutine(Move());

			}

		}

Then you could of course also set doMove to false when the last waypoint is reached.

The example scenes and scripts of the Pathfinding proejcts contain several scripts worth a look, AIBot and AIPath in particular.