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");
}
}
}
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");
}
}