Power up drooping from a destroyed brick is falling from the middle of the screen, I am trying to get the power up's to fall from the destroyed prick position.

private void OnCollisionEnter(Collision collision)
{          
    if (collision.gameObject.tag == "ball")
    {
        hitPoint--;
        if (hitPoint <= 0)
        {

            Destroy(this.gameObject);
            Instantiate(explosion, transform.position, transform.rotation);

            //calculate a random value between 0 and 1
            //if randomvalue is smaller than percentage do the next line
            if (randNum < percentage)
            {
               Instantiate(heart,Vector3.down * fallSpeed * Time.deltaTime, transform.rotation);
            }

        }
    }
}

1 Answers

0
Enz On

The problem is in this line:

Instantiate(heart,Vector3.down * fallSpeed * Time.deltaTime, transform.rotation);

which spawn the object in the middle of the screen that have to change like this:

Instantiate(heart, transform.position, transform.rotation);

Remember that OnCollisionEnter called only once in collision detection, also Instantiate method only creates an object and take position and rotation of the object. So you can't set the velocity for your object here. Attach another script to "heart powerup" and moving it by that script. For example:

public class Heart : MonoBehaviour
{
    private void Update()
    {
        transform.position += Vector3.down * fallSpeed * Time.deltaTime
    }
}