Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

685
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda