August 2018
Intermediate to advanced
794 pages
28h 4m
English
Our laser target should highlight the first object it hits – it shouldn't go through all of the objects ahead of it. The project's far clip plane defined the distance at which the projector stops. We can use a simple script to fire a Raycast, and use the distance to the first object it hits as a guide for setting this far clip plane:
using UnityEngine; public class LaserAim : MonoBehaviour { private Projector projector; private float margin = 0.5f; void Start () { projector = GetComponent<Projector> (); } void Update () { RaycastHit hit; Vector3 forward = transform.TransformDirection(Vector3.forward); if (Physics.Raycast(transform.position, forward, out hit)) ...Read now
Unlock full access