Search Unity

Highlighting an object when looking at it

Discussion in 'Documentation' started by OttoInteractive, Dec 23, 2020.

  1. OttoInteractive

    OttoInteractive

    Joined:
    Jul 29, 2020
    Posts:
    3
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Highlight : MonoBehaviour
    {
    public GameObject Area1;
    public float range = 100f;
    public Camera fpsCam;

    void Update()
    {

    RaycastHit hi;
    if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hi, range))
    {

    if (hi.transform.gameObject.layer == LayerMask.NameToLayer("Area1"))
    {
    Area1.GetComponent<Renderer>().material.color = new Color(0, 145, 255);
    Debug.Log("BRICK");
    }
    //Area1.GetComponent<Renderer>().material.color = new Color(255, 255, 255);
    if (hi.transform.gameObject.layer == LayerMask.NameToLayer("Default"))
    {
    Area1.GetComponent<Renderer>().material.color = new Color(255, 255, 255);
    }
    }

    }
    }
     
unityunity