Open door c#

i have this code:
when i enter a triggerzone and “use is pressed” i want the animation called open to play. but it isnt working. im quite a newbie and need to learn a lot :slight_smile:

public class doorOpen : MonoBehaviour
{
	public bool requireKey;
	public AudioClip doorSwishClip;
	private Animator anim;
	private GameObject player;
	private int open;
	//private PlayerInventory playerInventory;
	private int count;

	
	void Awake()
	{
		anim = GetComponent<Animator>();
	}
	
	void onTriggerEnter(Collider other)
	{
		if(other.gameObject.tag == "Player")
		{
			print("player is here");
			
			if(Input.GetButtonDown("Use") && open == 1)
			{
				open = 0;
				anim.animation.Play("Close");
			}
			else if(Input.GetButtonDown("Use") && open == 0)
			{
				open = 1;
				anim.animation.Play("Open");
			}
		}
	}
	
	void Update()
	{
		
		if(anim.IsInTransition(0) && !audio.isPlaying)
		{
			audio.clip = doorSwishClip;
			audio.Play();
		}
	}
}

K i’ll just try go through everything I’m noticing hopefully one of them might fix it for you!

So I’m not sure what kind of animation system you are using. But with my animations for my current project I’m using the Legacy system and a lot of this stuff you are doing seems excessive compared to what I do.

First of all I never need to GetComponent on awake or use an Animator variable. Maybe you are using mechanim and it’s different? Not sure for that.

You should already have the animations available if this script is attached to the door.

If it isn’t even printing “player is here” that means your trigger isn’t even working. Make sure you have colliders on both the door and the player objects.

On the player you will want to have a rigid body attached as well to the object with the collider, and set the collider IsTrigger = true.

That should make the collisions work since the moving object must have a rigid body.

Also if you are trying to get a button press you will need to use onTriggerStay() as the other user suggested. On Trigger Enter would force the player to press the “Use” button at the EXACT moment they enter the trigger, not even sure if this is possible since I think triggers and updates are not synced perfectly.

Not sure what you need the anim.IsInTransition for either. For a door opening or closing I generally would just do something like this in the Start Function.

animation[“Open”].wrapMode = WrapMode.Once;