- Home /
This question was
closed Nov 17, 2018 at 12:02 AM by
nthnlbaker for the following reason:
Solved it myself
Question by
nthnlbaker · Jul 31, 2018 at 08:58 AM ·
raycastpathfindingnavigationclick to moveclicktomove
Player won't move to a certain location clicked by the mouse
(EDIT 11/16/18: Issue resolved, was simply because of the agent's model being too big.)
Over the past couple of weeks I have been studying over Unity's pathfinding documentation in order to make a click to move player controller. It works, except that the player will not move to a location that has been clicked above it. It can move downwards perfectly fine, but for some reason it refuses to go up.
Script:
public class ClickToMove : MonoBehaviour {
//Variable for defining the Player's NavMeshAgent
private NavMeshAgent playerNav;
// Use this for initialization
void Start () {
//Get the Player's NavMeshAgent
playerNav = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
// Update is called once per frame
void Update () {
//Define Ray
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//If the lmb is clicked
if(Input.GetMouseButtonDown(0))
{
//If the ray hits the NavMesh
if(Physics.Raycast(ray, out hit, 100))
{
//Set Player's destination towards ray
playerNav.destination = hit.point;
}
}
}
}
If it helps, I'm using an Orthographic camera, and the variables that have been changed in my player's NavMeshAgent settings are as follows:
Base Offset - 6
Speed - 25
Angular Speed - 1000
Acceleration - 5000
If you need any more details I will gladly provide them.
Comment