En este momento estás viendo Obtener gameobject de RaycastHit

 – Unity

Obtener gameobject de RaycastHit – Unity

Obtener gameobject de RaycastHit

– UnityAssets3Free

hola , soy jorge y aqui os traigo
nueva pregunta

Estoy usando este código, pero lamentablemente recibo este error:

CS1061: Tipo UnityEngine.RaycastHit' does not contain a definition forgameObject’ y sin método de extensión gameObject' of typeSe puede encontrar UnityEngine.RaycastHit’ (¿falta una directiva de uso o una referencia de ensamblaje?

public float Selected;
public GameObject[] handler;
public float[] prices;
public GameObject Tile;
private Money mon;
// Use this for initialization
void Start () 
    mon =   GameObject.Find ("Gamelogic").GetComponent<Money>();


// Update is called once per frame
void Update () 
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if(Physics.Raycast (ray,out hit, 20))
    
        if(hit.transform.tag == "tiles")
        
            Tile = hit.gameObject;
        
        else 
        
            Tile = null;
        

    
    if(Input.GetMouseButtonDown(0) && Tile != null)
    

    
  
}

2 respuestas 2

Aquí hay una función que uso, debería poder adaptarla fácilmente.

GameObject GetClickedGameObject() 
 
    // Builds a ray from camera point of view to the mouse position 
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
    RaycastHit hit; 
    // Casts the ray and get the first game object hit 
    if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask)) 
    

        Instantiate (clickMarker,hit.point,Quaternion.identity); //places clickMarker at hit.point. This isn't needed, just there for visualisation. 
        return hit.transform.gameObject;
    
    else 
        return null; 

Creo que tu problema básico es que

Tile = hit.gameObject;

necesita ser

Tile = hit.transform.gameObject;

También:

if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask)) 

Preste atención a esta forma de hacerlo, tiene una máscara de capa incorporada, por lo que no necesita hacer su if (hit.transform.tag == «tiles»)

Más fácil y más corto, siempre puedes hacer:

hit.collider.gameObject.name

que devolverá la etiqueta de nombre del objeto que golpeó. Luego puede hacer verificaciones lógicas y lo que quiera hacer con esa información.

Más documentos aquí:

https://foro.unity.com/threads/getting-object-hit-with-raycast.573982/

nota: si aun no se resuelve tu pregunta por favor dejar un comentario y pronto lo podremos de nuevo , muchas gracias

por hoy,espero que te halla servido

Deja una respuesta