- Home /
Coin pickups MoveTowards player 2D C#
Hi I have a coin and a player. I want the coin to move towards the player when the player get's close enough and then destroy the coin.
Player has a rigidbody2D and a 2Dcollider The coin has a rigidbody2D but 2x 2Dcolliders - one big circle collider to check if the player is close enough then the coin moves towards the player and the second one to trigger when the coin collides with the player so it can pick it up/destroy it. I'm not sure if this approach is the correct one, but it's all I could think about.
Current state I'm in: player moves towards coin triggers big collider -> coin moves towards player and nothing. I'm unsure on how to differentiate between 2 colliders on 1 gameobject so I can't make the coin disappear when it hits the small collider. This is in 2D C#
public GameObject Collectibles;
public Transform Player;
private bool Triggered = false;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player") Triggered = true;
}
void Update()
{
if (Triggered == true)
Collectibles.transform.position = Vector2.MoveTowards(transform.position, Player.transform.position, Time.deltaTime);
}
Answer by Daviiid · Feb 12, 2021 at 11:38 PM
Figured it out. Made an empty gameobject as a child of the coin and added the second collider on it. Made another script on it to destroy it self on collision with the player.
Answer by VoidPhoenix96 · Feb 13, 2021 at 12:00 AM
I have a better thing you can do. Instead of using a second collider, use a`Physics.OverlapSphere` This should simplify everything.
I think it would increase performance (A little bit) and it would make your whole system a lot simpler.