Hey dude,
well its simple (because I was able to do it myself )
There are 3 thingsā¦
-
on ship I have collision detection (the same like you got on bricks etcā¦ void oncollision something), inside it there is a script that reads x of the ball and x of the ship. Difference of these values (the greater the further from the ship ball hits) is then multiplied with x vector of the speed of the ball. (thuss the ball bounces more sideways if its far from the ship center).
-
i also detect y speed in this collision detection. if its to low it means the ball goes to horizontally - this is the cause of horizontal loops (vertical loops are already solved by point 1). There is written something that if the y speed of the ball is below 5.0f then i hard set it to 5.0f at least. (I got the same on bricks in -5 vertical speed and +5 - because the ball can bounce to horizontaly from bricks as well in both ways - slightly going up, slightly going down). then i also hard set the y speed on -5 or +5 respectively (or maybe 10, iām not sure now).
-
finally i check absolute ball speed after the bounce. its absolute speed is Sqrt (XspeedXspeed + YspeedYspeed), I want the absolute speed of the ball be always a constant (10 works for me). I just adjust the X and Y speed of the ball by: ballabsolutespeed / 10 * (x and/or y speed).
the code is really a mess and iām sure it can all be done much more effectively, butā¦
THIS IS ON MY SHIP OBJECT:
void OnCollisionEnter2D(Collision2D collision)
{
//speedXY is bouncing speed in each axis - I just hard set both after the collision (maybe could use trigger instead)
speedX = 10 * (ball.transform.position.x - this.transform.position.x) + 0.5f * (Random.value - Random.value);
speedY = 8f - 7 * Mathf.Abs(ball.transform.position.x - this.transform.position.x) + 0.5f * (Random.value - Random.value);
// speedY is smaller if speedX is faster while speedX depends on x of ball - x of the ship
//Im sure random values are ********, but it was the first that worked for me before I got to know about random range, theyāre not even necessary as the variace of bounce is guaranteed by ball bouncin of ships more sideways if it hits off center)
// this sets higher y bounce if its to low - avoids horizontal loop
if (speedY < 5.0f)
{
speedY = 10.0f;
print(āOPRAVA LODIā);
}
// counting absolute speed of the ball
speedAbs = Mathf.Sqrt(speedX * speedX + speedY * speedY);
// recount so the absolute speed of the ball is always 10
speedX = ballspeed / speedAbs * speedX;
speedY = ballspeed / speedAbs * speedY;
//ballspeed is a public int (currently set to 10);
ball.GetComponent<Rigidbody2D>().velocity = new Vector2(speedX,speedY);
//final hard set of ball speed
}
And similar its done on a brick (I think its stupid and should all be on ball, but i just didnāt know how to find out if there is a collision of the ball with ship or with side of the playspace or with a brick). But well who cares, it works for now and later if I get smarter during the course I can fix it
Hope it helps!!!
Feel free to ask if it doesnāt work for u.