Our Target
object is a simple sphere object with the mesh render disabled. We have also created a point light and made it a child of our Target
object. Make sure the light is centered, or it will not be very helpful for us.
Look at the following code in the Target.cs
file:
using UnityEngine; using System.Collections; public class Target : MonoBehaviour { public Transform targetMarker; void Update () { int button = 0; //Get the point of the hit position when the mouse is being // clicked. if (Input.GetMouseButtonDown(button)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(ray.origin, ray.direction, out hitInfo)) { Vector3 targetPosition = hitInfo.point; targetMarker.position = targetPosition; } } } }
Attach this script to our Target
object, which is what we assign in the inspector to the targetMarker
variable. The script detects the mouse...