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!
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!
What scripting language are you using?
c++
can you put a trigger collider on it that triggers when the player crosses it so the chest opens & then instantiates the coins?
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.
void OnTriggerStay2D(Collider2D collider) {
//find out if this collider is attached to the player.
if (!collider.gameObject.CompareTag("Player")) {
return;
}
_levelInfo.InformantComplete();
}
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.
public void InformantComplete() {
if (_informantAwarded == false && _informantOn) {
_informantAwarded = true;
//award a star.
_playerInfo._starsGained++;
_starsUI.AddStar();
StarScroll();
}
}
public void StarScroll() {
GameObject scrollText = Instantiate(_starPrefab, _player.transform.position, _starPrefab.transform.rotation) as GameObject;
scrollText.GetComponent<StarText>().followTarget = _player;
scrollText.GetComponent<StarText>().GoStar();
}
StarScroll is a star that appears over the character. When it’s called, it automatically starts it’s animation.
public void GoStar() {
_animatorController.SetTrigger("Scroll");
}
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!
You’ll need to find a tutorial on mecanim.
This might help.
https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/animate-anything
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:
using UnityEngine;
using System.Collections;
public class AwakeOnDistance : MonoBehaviour
{
//[SerializeField] private Transform target; // The target objects wake up to
private GameObject player;
[SerializeField] private float distance = 100f; // Setting distance to target in the Inspector panel
//[SerializeField] private bool go_Sleep = false; // Is the object going back to sleep?
//[SerializeField] private float sleep_Distance = 0f; // Distance until object sleeps.
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag ("Player");
}
// Update is called once per frame
void Update () {
if (Vector2.Distance (transform.position, player.transform.position) < distance) {
GetComponent<Rigidbody2D>().WakeUp();
GetComponent<Rigidbody2D>().isKinematic = false;
}
}
}
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.
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.