Hey guys,
I don’t think this is going to be a tough question for you all. But, I spent an hour in just figuring out what to do. Well, to make things clear, I am making a Brick Breaker game and there is a Magnet power up. So, when I take the Magnet power up , then the ball sticks to my player but it doesn’t leave my player even though I’m adding a force.
void OnTriggerEnter(Collider other)
if (other.transform.tag == "Magnet")
{
magnetOn = true;
other.gameObject.SetActive (false);
}
}
void OnCollisionEnter(Collision other)
{
if (other.transform.tag == "Ball" && magnetOn == true)
{
ball.transform.parent = gameObject.transform;
ball.transform.position = new Vector3 (gameObject.transform.position.x, -8f, 0f);
ballInPlay = false;
ball.GetComponent<Rigidbody> ().isKinematic = true;
}
}
One more thing, my ball is a kinematic object.
Thanks in advance
void OnCollisionStay(Collision other)
{
if (Input.GetButton("Jump") && ballInPlay == false)
{
ball.transform.parent = null;
ballInPlay = true;
ball.GetComponent<Rigidbody> ().isKinematic = false;
ball.GetComponent<Rigidbody> ().AddForce (initialBallVelocity, initialBallVelocity, 0f);
}
}
}
I used @phxvyper sir's suggestion (putting the OnCollisionStay statements to the Update) and I added one more line before the "transform.parent = null" which is "magnetOn = false" so my ball is being launched but when the ball again collides with my paddle then it is not sticking to my paddle. Is anyone having an idea about this? Problem : How to keep the Magnet power up active for the whole scene?
– srikaran_p@phxvyper sir, if I don't make magnetOn = false, then the object is not being launched.
– srikaran_p