Hello,
I have a problem here. When I click down, my player moves towards to click like I want. But, the player doesn’t move to the exact point. He is off by a set amount. If I lower the offset, then I get very jaggety movement. Here is the code:
using UnityEngine;
using Pathfinding;
namespace RTSGame.Entities {
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(Seeker))]
[DisallowMultipleComponent]
public class Player : Entity {
//Private variables that will be used for moving the player
private Vector3 newPosition;
private CharacterController controller;
//Public variables that will be used for A* Pathfinding
[Range(0.5f, 5.0f)]public float distanceAwayFromWaypoint;
//Private variables used for A* pathfinding
private Seeker seeker;
private Path path;
private uint currentWaypoint;
private void Start() {
entity.tag = "Player"; //Sets the tag for the player
newPosition.y = GetHeight(true);//Make sure the player starts out flat on the ground
controller = entity.GetComponent<CharacterController>();
//A* Pathfinding
seeker = entity.GetComponent<Seeker>();
}
private void Update() {
Movement();
MovementLogic();
}
private void MovementLogic() { //Handles the movement logic for the player
if(Input.GetMouseButtonDown(1)) {
RaycastHit hit; //Creates a hit point for the ray
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //Gets the position of the mouse position and turns it into a ray
if(Physics.Raycast(ray, out hit)) { //Checks to see if anything was hit
if(hit.collider.tag != "Player") { //Makes sure the player can't move on himself
}
newPosition = entityTransform.position; //Set the "new" position equal to the current player's position
newPosition = hit.point; //Sets the "new" position based on where the ray hit
newPosition.y += GetHeight(true); //Makes sure the player won't sink into the ground on click
seeker.StartPath(entityTransform.position, newPosition, OnPathComplete); //Creates a path for the player to follow
}
}
}
private void Movement() { //Handles the actual player movement, along with a little basic A* Pathfinding stuff
if(path == null) //If there is no path to follow, stop the method
return;
if(currentWaypoint >= path.vectorPath.Count) //If the current waypoint is bigger or just as big as the amount of path nodes to follow, the stop the method
return;
Vector3 dir = (path.vectorPath[(int)currentWaypoint] - transform.position).normalized * speed; //Sets the direction the player has to move
controller.SimpleMove(dir); //Moves the player. No jumping will or can be added to game with "Simple Move"
if(Vector3.Distance(entityTransform.position, path.vectorPath[(int)currentWaypoint]) < distanceAwayFromWaypoint) //Checks to see if the player has reached the target waypoint
currentWaypoint++;
}
public void OnPathComplete(Path path) { //Gets called when the player has followed a node on the path
if(!path.error) { //Makes sure that there are NO path errors
this.path = path;
currentWaypoint = 0;
} else
Debug.LogError(path.error);
}
}
}
I cannot seem to find the problem here. Under the “Seeker” component’s settings, I even changed “Exact End Point” to “Closest On Node” and it didn’t work at all. I really cannot seem to figure out what the problem is. Please help me out! Thank you