Triggering animations in set areas by Key Press?

Hey all, I’m pretty new to Unity and am in the middle of trying to figure this out. I have a chest that I want to open by pressing the “E” key, but I only want this to work when I am right up against the chest. So, in theory, I have a chest with a trigger in front of it so that when I walk into that area, the trigger is activated and an attached script is activated. Then, I want it to be that once I hit the “E” key, the chest will open and the trigger will destroy itself. This is what I have so far, but I’m missing the piece with the “E” key…

function OnTriggerEnter (col : Collider) { if(col.gameObject.tag == "Player") { treasureChest.GetComponent.<Animation>().Play(); Destroy(gameObject); %|919537274_4|% }

Any ideas?

If your new to Unity, I stringly advice to learn C# instead of Javascript. C# code is much cleaner and more object oriented!

For you problem, I can give you a head start with a C# example:

    protected bool PlayerInRange;

    protected void Update()
    {
        if (PlayerInRange && Input.GetKeyDown(KeyCode.E))
        {
            // open the treasure chest
            treasureChest.GetComponent<Animation>().Play();
        }
    }

    protected void OnTriggerEnter(Collider otherCollider)
    {
        if (otherCollider.CompareTag("Player"))
        {
            PlayerInRange = true;
        }
    }

    protected void OnTriggerExit(Collider otherCollider)
    {
        if (otherCollider.CompareTag("Player"))
        {
            PlayerInRange = false;
        }
    }