Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

simple chest opening animation onkeypress

Discussion in '2D' started by civeo, Jul 20, 2015.

  1. civeo

    civeo

    Joined:
    Feb 1, 2015
    Posts:
    6
    Hey, this question has probably been asked before (and yes I have search for this problem on google & forum and still haven't found the right fix yet).
    But the question is easy: i'm simply trying to make an animation trigger when my player is colliding with the object box/circle collider and when i'm pressing the interact button.
    An example for this is where I try to start an animation for a chest to open.
    I try to achieve this with the following code:

    public class OpenChest : MonoBehaviour {
    private Animator anim;

    void Start () {
    }

    void Awake() {
    anim = GetComponent<Animator> ();
    anim.SetBool ("opencanim", false);
    }

    void OnTriggerEnter2D(Collider2D other)
    {
    if ((other.name == "Player") && (Input.GetButtonDown ("Interact"))) {
    anim.SetBool("opencanim", true);
    }
    }
    }

    It works when I remove "input.getbuttond...." command but that just start the animation when the player and chest collider touch each other, but I want it to be by a buttonpress which does not work in this code.
    This script is attached to the chest object in the game, maybe that's why it doesn't work? Cause it doesn't refer correctly to the inputcontroller? I'm just guessing here maybe you guys can shed some light on this?
    Also the "interact" button is setup right so that can't be the problem.
     
  2. Zk

    Zk

    Joined:
    May 25, 2013
    Posts:
    19
    I think your problem is that Input.GetButtonDown("Interact") will only return true during the frame that the button was pressed, and since you've put it in the OnTriggerEnter2D function that's unlikely to be the exact same frame. You would want to put the check for Input.GetButtonDown in the Update() function.

    You could probably do something like this:

    Code (CSharp):
    1. public class OpenChest : MonoBehaviour {
    2.  private Animator anim;
    3.  private bool touched; // Use this to indicate if the player touched the trigger
    4.  
    5.  void Awake() {
    6.   anim = GetComponent<Animator> ();
    7.   anim.SetBool ("opencanim", false);
    8.   touched = false;
    9.  }
    10.  
    11.  void Update() {
    12.   if ((touched) && (Input.GetButtonDown ("Interact"))) { // Check for that button and the player having already entered the trigger
    13.    anim.SetBool("opencanim", true);
    14.   }
    15.  }
    16.  
    17.  void OnTriggerEnter2D(Collider2D other) {
    18.   if (other.name == "Player") {
    19.    touched = true;  // Set this to true when the player touches this object
    20.   }
    21.  }
    22. }
    You might also want to disable "touched" if the player leaves the trigger, depending on your design.

    Alternatively you could just check for Input.GetButton instead of Input.GetButtonDown, which will return true for as long as the button is held down.
     
  3. JustinTheDev

    JustinTheDev

    Joined:
    Jul 23, 2015
    Posts:
    3
    Forgive me, but I'm not at my computer so I can't test these, but I have two possible solutions,

    The first is instead of using OnTriggerEnter2d try OnTriggerStay2D as this will return each frame you are in contact with it.

    The other is have a bool that determines if you can open the chest. OnTriggerEnter2D set it to true and OnTriggerExit2D set it to false. When the player tries to open the chest, check the bool. If true open, if false do not open.
     
    Last edited: Jul 24, 2015