- Home /
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 :)
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++;
}
}
}
Answer by stulleman · Aug 20, 2014 at 02:47 PM
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
I altered quite a lot of my code to try and get this working but it doesn't. I never declare 'dir' so I'm not sure what that's doing.
It stores the direction of the next waypoint and thereby, where to move, based on teh current position.
stulleman's code should work as it stands.
Have you seen my edit? $$anonymous$$aybe you tried the old version, where I forgot the direction.
Hmmm, I'm not sure where you're getting the 'nextWaypointDistance' from it's never assigned anything so it's always 0
Answer by Cherno · Aug 20, 2014 at 02:49 PM
Well, your script doesn't even include any movement code, so that would help a lot ;)
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.
This didn't work, my character would only move to the next waypoint.
That's probably because the maxWaypointDistance is too low.
If no one here can help you, you really should look at the example scripts provided with the project, there is one very simple that has a proper function for moving and speed. I think it's AIFollow.
Simple moving and speed isn't the problem, it's getting that working with Raycasting and A*Pathfinding that I'm struggling with.
It's not the maxWaypointDistance
Okay. Have you looked at the example script? You only need to put the Vector3 you get from your raycast into the script's targetPosition variable.
So I've updated my code since the answers above and I've got my guy beginning to move along the waypoints, but he stops half way along the path and I get a constant error stating "".
I suspect that the issue is I'm updating the currWaypoint before he is actually reaching it. Any help would be appreciated!
using UnityEngine;
using System.Collections;
using Pathfinding;
public class PlayerPather : $$anonymous$$onoBehaviour {
public GameObject theCamera;
public RayCast cC;
Seeker seeker;
Path path;
int currentWaypoint;
float nextWaypointDistance;
float speed = 5f;
// 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;
}
Vector3 currTarget = path.vectorPath[currentWaypoint];
Vector3 dir = (currTarget - transform.position).normalized;
nextWaypointDistance = Vector3.Distance(dir, currTarget);
transform.position += dir * speed * Time.deltaTime;
if(Vector3.Distance(transform.position, currTarget) < nextWaypointDistance) {
currentWaypoint++;
}
if(currentWaypoint >= path.vectorPath.Count) {
return;
}
}
}