Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

689
Vistas
Unity: On mouse click onto box collider

I have a quick question, it seems pretty simple but I can't get my head around. I am trying to click on a door by mouse click which will then open. So basically an animation will play after mouse click, this is all working already in my project but unfortunately the door will open wherever I click but it should obviously only open when I click on the door. I made a box collider around the door and set it to trigger but it doesn't fix my problem.

Would anyone be able to help? I would really appreciate it.

Here is my current code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTor : MonoBehaviour
{
  private void Update()
  {
      if(Input.GetMouseButtonDown(0))
      {
          GetComponent<Animator>().SetTrigger("Tor");
      }
  }
}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Input.GetMouseButtonDown(0) does not check if you are currently clicking on your door, and is simply fired whenever you click when your game run (wherever on the window)

I suggest you to look at this thread here, which implies the use of Raycasts

over 4 years ago · Santiago Trujillo Denunciar

0

If you have a 3D scene, you propably need to use raycast: Try

if(Input.GetMouseButtonDown(0))
{
    RaycastHit hit;
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out hit))
    {
        // Check if hit.transform is door, 
        if(...) GetComponent<Animator>().SetTrigger("Tor");
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda