Greetings Unity community,
I’m currently facing a puzzling issue with my door animation. Upon pressing the “Open” button, the door animation starts spamming, rapidly opening and closing instead of smoothly staying in the open position. I’m seeking assistance to troubleshoot and resolve this unexpected behavior.
Problem:
-
Animation Spam: When the “Open” button is pressed, the door animation goes into a rapid open-close loop instead of smoothly opening and staying in the open position.
-
Fails to Stay Opened: Despite triggering the “Open” animation, the door does not remain in the open state as expected.
Code snippet:
using UnityEngine;
public class DoorScript : MonoBehaviour
{
public Transform PlayerCamera;
[Header("MaxDistance you can open or close the door.")]
public float MaxDistance = 5;
private bool opened = false;
private Animator anim;
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
Pressed();
}
}
void Pressed()
{
RaycastHit doorhit;
if (Physics.Raycast(PlayerCamera.transform.position, PlayerCamera.transform.forward, out doorhit, MaxDistance))
{
if (doorhit.transform.tag == "Door")
{
anim = doorhit.transform.GetComponent<Animator>();
opened = !opened;
anim.SetBool("Opened", !opened);
}
}
}
}
I kindly request your guidance in identifying the root cause of the animation spamming issue and the door’s failure to stay in the open position. Any suggestions, code insights, or debugging tips would be immensely helpful in resolving this animation glitch.
Thank you in advance for your valuable assistance and expertise