About 'Coin Pickups'!

In this video (objectives)…

  1. Import and animate coins.
  2. Create mechanism where the coins are picked up and destroyed.

After watching (learning outcomes)…

Start the process of creating a coin pickup which destroys when player touches it.

(Unique Video Reference: 29_TV_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

1 Like

I felt this was a very straightforward lesson and enjoyed it.

Working on changing the game a little I want the player to be unable to see the exit until all coins have been collected. This was easy in my mind, I was adding a coinCounter to the AddToScore routine. It all works However I discovered there are times in the game where I get two hits instead of one on the trigger. I believe it is due to the player having two colliders on it.

What would be the best work around for this or am I trying to go a wrong direction in this change.?

Thanks I know it is outside of the course but am not sure the way the player is set up is best. Perhaps one of the colliders would be better on a separate object where the position matches the player transform. Other thought wouldbe perhaps have the feet colider (or the body) be on a child object of the player. I know both would require code changes just not sure what is best.

John P

PS I believe the 2nd trigger occurs when I jump down on a coint cauing the feet to go through the coin with the body.

I think I found my workaround. I added a child object to the player called it CoinCollector mad a new collision level in the game called it coins. Placed the CoinCollector and the Coins on the Coin level and limit collision only to coin on coin level collision.

Tested by first run game with child object inactive no coins hit.
Activate the child I get the hits.

Pretty sure this will do the trick.

2 Likes

Good job John, glad you’re enjoying the course and thanks for keeping us in the loop re your progress.

Thank you John! I thought I was going mad with this issue!!!

I Followed your advice and created a child object for my Player which I called CoinCollector, put a capsule collider on it (making it around the same size of player), then I added this layer to intractables and changed Physics2D settings so that only Interactables could collide with Interactables (removed player from being able to interact with Interactables) and it works a treat!!!

1 Like

Hey. I had this issue as well, and I was going for the same route as John, but I realized that there is a bit of a simpler fix - just add a boolean, eg “alreadyPickedUp”, set it as false on initialization, and check if its false OnTriggerEnter2D, if so, then set it as true. Of course it doesn’t solve the double collider trigger issue, but I feel like as simple as it gets on when it comes to fixing this.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CoinPickup : MonoBehaviour
{
    [SerializeField] AudioClip coinPickupSFX;
    [SerializeField] int value = 50;
    bool alreadyPickedUp = false;

    private void OnTriggerEnter2D(Collider2D other) {
        if (!alreadyPickedUp)
        {
            alreadyPickedUp = true;
            Destroy(gameObject);
            AudioSource.PlayClipAtPoint(coinPickupSFX, Camera.main.transform.position);
            FindObjectOfType<GameSession>().AddToScore(value);
        }
    }
}

Privacy & Terms