top of page
Search

Good Pickup, bad pickup

  • Writer: Keshav Batra
    Keshav Batra
  • Jan 27, 2023
  • 1 min read

This took me a little while to figure out but I finally got it.



As you can see at the coin amount section of the player, whenever I pickup an object the amount increases or decreases depending on the pickup. Below is the code in which I used to make this happen.


private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player") //Senses the player
        {
            if (IsCollidingWithGoodPickup == true) 
            {
                other.GetComponent<PlayerMovement>().CoinAmount++; //Adds 1 to the amount of coins 
                transform.position = Lanes[Random.Range(0, 3)].transform.position; //Setting the players position with the random lanes position
            }

            else
            if (IsCollidingWithBadPickup == true)
            {
                other.GetComponent<PlayerMovement>().CoinAmount--;
                transform.position = Lanes[Random.Range(0, 3)].transform.position;
            }
        }
    }

I created two booleans called IsCollidingWithGoodPickup and IsCollidingWithBadPickup and set them as both true so that the player could collide with them. I then used an else statement to make sure that they're collecting them separately. If the player collects the good pickup then the amount adds and if it collects the bad pickup then it subtracts. I then went to the pickups and enabled the booleans in there.

 
 
 

Comments


bottom of page