Skip to content

Instantly share code, notes, and snippets.

@niiicolai
Last active January 3, 2024 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niiicolai/f815e98c7d38b39947738b68fc8fa1eb to your computer and use it in GitHub Desktop.
Save niiicolai/f815e98c7d38b39947738b68fc8fa1eb to your computer and use it in GitHub Desktop.
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);
}
}
}
}
}
@Tydoubleyou-pixel
Copy link

Is there a way to increase the speed of the ray cast?

@niiicolai
Copy link
Author

niiicolai commented Jul 28, 2023

Hi @Tydoubleyou-pixel. I'm not sure what you mean by the speed of the raycast. A raycast is just a check from an origin (fx Vector3(0, 0, 0)) towards some direction (fx Vector3(0, 0, 1)) and a length (fx 1). Speed is not a parameter you can pass to the method. You can read about the options of raycast here: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

@YuneekGames
Copy link

YuneekGames commented Jan 3, 2024

If you mean the travel speed of movement from the original position to raycasted MousePosition then you adjust the agent speed in the inspector

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment