Skip to content

Instantly share code, notes, and snippets.

@niiicolai
Last active August 4, 2022 20:23
Embed
What would you like to do?
using UnityEngine;
using UnityEngine.AI;
public class Movement : MonoBehaviour
{
[SerializeField]
private Camera camera;
private string groundTag = "Ground";
private NavMeshAgent agent;
private RaycastHit hit;
// Called when a script is enabled
void Start()
{
agent = GetComponent<NavMeshAgent>();
}
// Called once every frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if (hit.collider.CompareTag(groundTag))
{
agent.SetDestination(hit.point);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment