1 Question: How to add a Jump Force when I collect a coin

question created at Sat, Jun 1, 2019 12:00 AM

So I have a coin in my scene right now I am it set up as a Trigger and to Disappear when I run into it, i have functional code that always me to Jump when I press space, but I want to only be able to jump after I collect the coin.

Well I don't really know where to start, I'm fairly new started coding 2 days ago. I can't seem to find anything on YouTube.

//-----------This Code allows the coin to disappear when collected-----------

{

    private void OnTriggerEnter(Collider plyr)
    {
        if (plyr.gameObject.tag == "Player")
            gameObject.SetActive(false);
    }

}
//---------------------------This is Jump----------------------------------

 if (onGround)

          {
              if (Input.GetButtonDown("Jump"))
              {
                rb.velocity = new Vector3(0f, 50f, 0f);
                  onGround = false;
              }
          }

      }

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.CompareTag("Ground"))
        {
            onGround = true;

        }
    }
0
  1. In your player controller, create a public Void Jump() {} method. Move your jumping code in there and put Jump(); in the if..."Jump")) block. Then in your coin code, in the if...tag == "Player") block, do gameObject.GetComponent<PlayerController>().Jump(); or whatever your player controller script is called instead of PlayerController.
    2019-06-01 05:15:50Z
1 Answers 1

Define a bool to keep track if a coin is collected and an OnTriggerEnter method in your Player script. I'm assuming you have a tag for coin objects which is called "Coin".

private bool coinCollected = false

private void OnTriggerEnter(Collision other)
{
    if(other.gameObject.CompareTag("Coin"))
    {
        coinCollected = true;
    }
}

Then modify your jump method:

 if (onGround)

      {
          if (Input.GetButtonDown("Jump"))
          {
            rb.velocity = new Vector3(0f, 50f, 0f);
            onGround = false;
            coinCollected = false;
          }
      }

  }
0
2019-06-05 14:30:50Z
source placed here