Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

684
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!