Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

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