Last active
January 3, 2024 05:28
-
-
Save niiicolai/f815e98c7d38b39947738b68fc8fa1eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} | |
} |
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
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