- Home /
Breakout clone.
I'm cloning a classic game called "Breakout" where the objective of the game is to destroy all bricks. I have made a twist to spice up the game. I created 8960 bricks and if the ball collides with a brick, the brick then turns to a ball. The ball has 2d circle collider and a 2d rigidbody attached. It also has 2d physics material attached on the collider with bounciness of 1 and friction of 0. Now, the problem is some of the balls does not have the same angle when instantiated. And some of the balls are just bouncing straight horizontally and vertically.
Here's the code attached on the ball
function Start(){
if(Random.Range(0,2)<=0.5)
rigidbody2D.AddForce(Vector2(128,-96));
else
rigidbody2D.AddForce(Vector2(-128,-96));
}
function OnCollisionEnter2D(col:Collision2D){
if(col.gameObject.tag=="Player")
gameObject.layer=8;
}
function Update(){
if(transform.position.y<=-5)
Destroy(gameObject);
}
and here's the code for
function OnCollisionEnter2D(col:Collision2D){
if(col.collider.tag=="Respawn"){
Destroy(gameObject);
Instantiate(Resources.Load("Red"),Vector2(transform.position.x,transform.position.y),transform.rotation);
}
}
Note: here's my layer matrix.
Answer by robusto · Jun 08, 2014 at 04:56 PM
When you instantiate the object, if you instantiate touching, overlapping, or inside another object it will instantly react volatile, you may want to make sure where you are instantiating/creating the object is clear/in empty space. So you are creating a new ball inside the original ball, aka the position of the ball is the same as the original ball which is giving the unpredictability of the physics. Try instantiating with an offset on the x or y position.
float offset = 1f;
Instantiate(Resources.Load("Red"),Vector2(transform.position.x,transform.position.y + offset),transform.rotation);
Your answer
Follow this Question
Related Questions
Character Controller meets Rigidbody 1 Answer
How to tell if my character hit a collider of a certain layer 1 Answer
Finding collision force not working in certain situations. 0 Answers
Calculate Force of WheelCollider hitting the ground? 1 Answer
Click and drag object, apply force to other objects 1 Answer