Setting up the player tank and aspect
Our Target
object is a simple sphere game object with the mesh render removed, so that we end up with only the Sphere Collider.
Look at the following code in the Target.cs
file:
using UnityEngine; public class Target : MonoBehaviour { public Transform targetMarker; void Start (){} 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; } } } }
Note
You'll notice we left in an empty Start
method in the code. While there is a cost in having empty Start
, Update
, and other MonoBehaviour
events that...