[Help me please] Raycasting trigger all objects' animations in the same tag while I only hitting one box collider
I am currently trying to play drawer open animations separately when I press E button. However, when I actually pressed down E button, all animations using the same tag played at the same time. All three drawers are using the same script, but separate animations, in the animators they use the same bool "open" as parameter. Can anyone please help me with this problem? Does it mean that I need to do several copies of my scripts and change the animator parameter then reassign to the drawers separately? Or is there anything wrong with my code? Thank you in advance.
this is my script, all three drawers are marked as "Selectable":
using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI;
public class draweropener : MonoBehaviour {
public GameObject other;
[SerializeField] private string selecttag = "Selectable";
public Image aiming;
private bool _isopen = false;
public GameObject OpenPanel = null;
public Animator ani;
public string OpenText = "Press 'E' to open";
public string CloseText = "Press 'E' to close";
private Transform _selection;
private void Update()
{
Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward *5f, Color.red, 0.5f);
aiming.GetComponent<Image>().color = Color.white;
if (_selection != null)
{
_selection = null;
}
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 5f))
{
if (hit.collider.tag == "Selectable")
{
aiming.GetComponent<Image>().color = Color.red;
OpenPanel.SetActive(true);
UpdatePanelText();
Debug.Log(hit.collider.name);
if (IsOpenPanelActive)
{
if (Input.GetKeyDown(KeyCode.E))
{
_isopen = !_isopen;
Invoke("UpdatePanelText", 1.0f);
UpdatePanelText();
other.GetComponent<Animator>().SetBool("open", _isopen);
}
}
}
}
else
{
OpenPanel.SetActive(false);
}
}
private void UpdatePanelText()
{
TextMeshProUGUI panelText = OpenPanel.transform.Find("Text").GetComponent<TextMeshProUGUI>();
if (panelText != null)
{
panelText.text = _isopen ? CloseText : OpenText;
}
}
private bool IsOpenPanelActive
{
get
{
return OpenPanel.activeInHierarchy;
}
}
}
Answer by Chahorain · Nov 04, 2019 at 11:49 PM
And there is a new problem, how do I detect I move off from an object? my ui text keep updating until i turn around and move far away...