Question by
unity_ij-uBygkHoAdhw · Apr 07, 2021 at 04:29 PM ·
c#raycast
Detect RayCast hit,Selecting specific GameObject in Forest
How do I detect on the GameObject itself if it has been hit with a RayCast? I am trying to do a thing where I can click on a group of trees, or a square on my 3d grid, and have it open a gui that tells me information about it. Here is my current code that tells me the name of the prefab it hits, but I am trying to make a script now that detects if it was hit with a raycast:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectClicker : MonoBehaviour
{
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100.0f))
{
if (hit.transform != null)
{
PrintName(hit.transform.gameObject);
}
}
}
}
private void PrintName(GameObject go)
{
print(go.name);
}
}
How would I make a script for the prefab side that can read off information about it? Like, have a script that allows editable text on a prefab, then when a RayCast hits it, it can open a GUI and display the information on that script?
Comment