Reputation: 325
I have this table drawer I am trying to animate, where if the player presses "E", the drawer opens and stays open. And when the player presses "E" again, the door closes. The script is based on the player entering a trigger and I have it on multiple doors in my scene. I'm running into a few issues though, with the drawer when the player presses "E" the collider on the door moves but the drawer gameObject does not. I have come to the conclusion that it's not script because its working finds on the doors, but I have tried everything else. I will attach some images for reference.
public class DoorScript : MonoBehaviour
{
public GameObject OpenPanel = null;
private bool _isInsideTrigger = false;
public Animator _animator;
public string OpenText = "Press 'E' to open";
public string CloseText = "Press 'E' to close";
private bool _isOpen = false;
private void Start()
{
OpenPanel.SetActive(false);
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
_isInsideTrigger = true;
OpenPanel.SetActive(true);
UpdatePanelText();
}
}
private void UpdatePanelText()
{
Text panelText = OpenPanel.transform.Find("Text").GetComponent<Text>();
if (panelText != null)
{
panelText.text = _isOpen ? CloseText: OpenText;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
_isInsideTrigger = false;
OpenPanel.SetActive(false);
}
}
private bool IsOpenPanelActive
{
get
{
return OpenPanel.activeInHierarchy;
}
}
// Update is called once per frame
void Update()
{
if (IsOpenPanelActive && _isInsideTrigger)
{
if (Input.GetKeyDown(KeyCode.E))
{
_isOpen = !_isOpen;
Invoke("UpdatePanelText", 1.0f);
_animator.SetBool("open", _isOpen);
}
}
}
} //Credit Jayanam Youtube Channel for Script
Upvotes: 0
Views: 1777
Reputation: 1588
In order to take advantage of static batching, you need to explicitly specify that certain GameObjects are static and do not move, rotate or scale in the game. To do so, mark GameObjects as static using the Static checkbox in the Inspector
You can make the object animate again when you uncheck the static checkbox. Good thing you put the images ahead otherwise this problem would have been difficult to resolve.
Note: You can disable the static batching on entire Table or you can disable static batching on the child i.e. the Drawer. Disabling the static batching on child would be more beneficial I believe but if some problem in animation comes then might as well try disabling static batching on entire object and see if it suits your need.
Upvotes: 1