- Home /
This question was
closed Aug 28, 2021 at 05:52 PM by
andrew-lukasik for the following reason:
The question is answered, right answer was accepted
Question by
unity_DKZaovdDe7eF6A · Aug 11, 2021 at 04:44 PM ·
raycastselecting objects
How to select objects with raycasts?
Im trying to convert a Transform
into a custom type:
public class SceneMgr : MonoBehaviour
{
public Building selectedBuilding;
void HandleTerrainClicks()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
Transform target = hit.transform;
selectedBuilding = target(???);
SelectedBuildingSettings();
}
how do I do this?
Comment
Best Answer
Answer by andrew-lukasik · Aug 11, 2021 at 04:47 PM
if( Input.GetMouseButtonDown(0) )
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if( Physics.Raycast(ray,out RaycastHit hit,1000f) )
{
Building hitBuilding = hit.collider.GetComponentInParent<Building>();
if( hitBuilding!=null )
{
selectedBuilding = hitBuilding;
SelectedBuildingSettings();
}
}
}