To create a point-and-click game using a Raycast, do the following:
- Remove the Tile Manager (Script) component from the Main Camera GameObject.
- Create a new 3D Sphere, named Sphere-destination, scaled to (0.5, 0.5, 0.5).
- Create a new Material object that's red and assign it to the Sphere-destination GameObject.
- Create a new MoveToClickPoint C# script class containing the following and add an instance object as a component to the Sphere-arrow GameObject:
using UnityEngine;
using UnityEngine.AI;
public class MoveToClickPoint : MonoBehaviour {
public GameObject sphereDestination;
private NavMeshAgent navMeshAgent;
private RaycastHit hit;
void Start() {
navMeshAgent = GetComponent<NavMeshAgent>();
sphereDestination.transform.position = transform.position;
}
void Update() {
Ray rayFromMouseClick = Camera.main.ScreenPointToRay(Input.mousePosition);
if (FireRayCast(rayFromMouseClick)){
...