Make object fall down and start again from top.
Hi, I am pretty new to Unity and I have been looking for an answer to this quistion for an hour without success. I am programming a 2d game btw. I would like to have my bullet go down (vertically) until it reaches the end of the screen or it hits the player. If it reaches the end of the screen it should come back at the top but with a different (random) x coordinate. Thanks! Laurence
Answer by Rhyusaky · Apr 13, 2017 at 12:50 PM
You can try to put a collider where it would be this screen end, then when it collides with some object, you make that object receive a random position value, if it is only in y, you can put it to the bullet script, or Bullets manager to test if she is passing this limit y, there something like this:
void OnCollisionEnter(Collision coll)
{
foreach(var contact in coll.contacts)
{
GameObject collidedObject = contact.otherCollider.gameObject;
float top = anyValue;
collidedObject.transform.position.y = top;
float minX=any,maxX=any;
collidedObject.transform.position.x = Random.Range(minX,maxX);
}
}
Thanks a lot!!! The problem why $$anonymous$$e wasn't working was that I didn't have a collider at the bottom