I have a simple setup with a terrain, a baked navmesh, and a capsule with a navmesh Agent, and the script below. What's supposed to happen is the user clicks a spot of the terrain, and the capsule navigates there around the obstacles.
Everything seems to work fine for the most part, but if the user clicks a flat part of the terrain (rather than an unwalkable area like a tree), the raycast doesn't hit, so the destination doesn't get updated (seen by the debug log "hit"). I'd like to be able to click walkable areas as well.
As this code works fine in other scenes, I doubt it's an issue with the code, and likely my terrain set up.
public class PlayerController : MonoBehaviour
{
NavMeshAgent navMeshAgent;
//public Transform target;
// Start is called before the first frame update
void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
//navMeshAgent.SetDestination(target.position);
SetTargetByMouseClicking();
}
void SetTargetByMouseClicking()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit,100f))
{
Debug.Log("HIT");
navMeshAgent.SetDestination(hit.point);
}
}
}
}