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

How to make a chest open by itself when the character passes near it?

Discussion in '2D' started by Dumitru, Jul 6, 2015.

  1. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80
    Hello, so I'm making a 2D game , how could I make the chest open itself when a character passes by it and make coins fall out of it?

    All Ideas and Tips are welcome , thank you for taking your time to read this post!
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    What scripting language are you using?
     
  3. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80
    c++
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    can you put a trigger collider on it that triggers when the player crosses it so the chest opens & then instantiates the coins?
     
  5. itzclay36

    itzclay36

    Joined:
    Apr 11, 2015
    Posts:
    25
    I add an empty gameobject to that object. Then on the empty gameobject, put a collider on it and make it the area you are wanting it to open for the player. Set it as a trigger.

    Then create a new script that will detect when the collider has triggered, which is will trigger off teh animation of making coins fall out.

    Here's a sample script where I do something similiar.

    Code (CSharp):
    1.  
    2.  
    3.     void OnTriggerStay2D(Collider2D collider) {
    4.         //find out if this collider is attached to the player.
    5.         if (!collider.gameObject.CompareTag("Player")) {
    6.             return;
    7.         }
    8.         _levelInfo.InformantComplete();
    9.     }
    This is basically the completion of an objective in my game. As you can see, it checks the tag of what ti's colliding with, and if it's not the player it exits. Otherwise, it calls another object and does all the objective complete stuff.

    Here is another script that handles the objective stuff. It first makes sure it hasn't been completed already(you'll need to do that or it will keep dishing out coins). Then it adds everything and instantiates scrolling combat text object I created.

    Code (CSharp):
    1.     public void InformantComplete() {
    2.         if (_informantAwarded == false && _informantOn) {
    3.             _informantAwarded = true;
    4.             //award a star.
    5.             _playerInfo._starsGained++;
    6.             _starsUI.AddStar();
    7.             StarScroll();
    8.         }
    9.     }
    10.  
    11.     public void StarScroll() {
    12.         GameObject scrollText = Instantiate(_starPrefab, _player.transform.position, _starPrefab.transform.rotation) as GameObject;
    13.         scrollText.GetComponent<StarText>().followTarget = _player;
    14.         scrollText.GetComponent<StarText>().GoStar();
    15.     }
    16.  
    StarScroll is a star that appears over the character. When it's called, it automatically starts it's animation.

    Code (CSharp):
    1.  
    2.     public void GoStar() {
    3.         _animatorController.SetTrigger("Scroll");
    4.     }
     
    Last edited: Jul 6, 2015
  6. Dumitru

    Dumitru

    Joined:
    May 7, 2015
    Posts:
    80

    Thanks!
    Although I'm a bit lost, can you explain a bit more in depth?
    Also, How do I make the animation to trigger? I'm still getting used to the Unity interface, thank you for helping me! :)
     
  7. itzclay36

    itzclay36

    Joined:
    Apr 11, 2015
    Posts:
    25
  8. WebMagi

    WebMagi

    Joined:
    May 24, 2015
    Posts:
    23
    If only the Rigidbody 2D component had the ability to wake an object on distance, you could simply set the Sleeping Mode for your chest to "Start Asleep", and then wake it when your character passes by. The 2D community did help me come up with a script for this, but it's in C#. Maybe a C++ dev could re-purpose it for you? Here is the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AwakeOnDistance : MonoBehaviour
    5. {
    6.     //[SerializeField] private Transform target;            // The target objects wake up to
    7.     private GameObject player;
    8.     [SerializeField] private float distance = 100f;       // Setting distance to target in the Inspector panel
    9.     //[SerializeField] private bool go_Sleep = false;       // Is the object going back to sleep?
    10.     //[SerializeField] private float sleep_Distance = 0f;   // Distance until object sleeps.
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         player = GameObject.FindGameObjectWithTag ("Player");
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if (Vector2.Distance (transform.position, player.transform.position) < distance) {
    20.             GetComponent<Rigidbody2D>().WakeUp();
    21.             GetComponent<Rigidbody2D>().isKinematic = false;
    22.         }
    23.     }
    24. }
    I've commented out features I have yet to figure out how to code. Also, the game objects I needed to awaken were touching, so I set "Is Kinematic" to true in the Rigidbody 2D component, and then changed it to false in the script for when the object awakens.

    Hope this helps.

    Edit: I forgot to mention that you can just remove/comment out the "isKinematic" line.
     
    Last edited: Jul 8, 2015
  9. WebMagi

    WebMagi

    Joined:
    May 24, 2015
    Posts:
    23
    After reviewing the video recommended by @itzclay36, it does look like Mecanim and triggers would be a better solution than a simple wake on distance, as my script really only activates physics.
     
    theANMATOR2b likes this.