Hey, so I’m trying to make a breakout style game. I’m not using physics for it, and I’m struggling with how to get the ball to change the bounce based on where it hits on the paddle. Normally with one of these types of games, the angle changes, depending on how far from center the ball bounces, and which direction of the center it bounces.

Here is my movement code.

``````void Update()
{
if (!moving)
{
if (Input.GetMouseButtonDown(0))
{
xVel = Random.Range(-20, 20);
zVel = Random.Range(2, 10);
moving = true;
}
}

moveSpeed.x = xVel;
moveSpeed.y = 0;
moveSpeed.z = zVel;
transform.Translate(moveSpeed * Time.deltaTime);

}
``````

When I bounce against a wall I am basically just changing the x or z direction by multiplying by -1:

``````if (otherObject.tag == "Wall")
{
xVel *= -1;
}
``````

Is there a way to get it to recognize where it is hitting the paddle this way, or am I going to need to use physics somehow?

Thanks[/code]

Well, where is your section that specifies when it hits the paddle?

``````if(otherObject.tag == "paddle")
``````

Also, are you using OnCollision or OnTrigger? As they provide different info?

If you’re using OnTrigger, you could check if its the paddle, if so, calculate the distance from the paddle center and the ball position and multiply that by some number.

Ex-

``````if(otherObject.tag == "paddle"){

//Figure out if it hit on the far side or near side of the paddle.

if(xVel > 0)
ballDist = transform.position.x - otherObject.transform.position.x;

else if(xVel < 0)
ballDist = otherObject.transform.position.x - transform.position.x;

//What that gives you, is the distance from the paddle center you're hitting, and its negative or positive, which dictates whether you hit the far side(positive) or the near side(negative).

//Then we change direction based on the percent of possible distance we hit the paddle on

//Change the xVel accordingly

xVel *= percentDist;

//If the ball hits on the near side it switches direction, else it stays the same direction.  The further away it hits, the more it keeps the same speed
``````

If all of that doesn’t make sense, or if my solution just actually flat out doesn’t work, let me know. Hopefully this helps. And this almost makes me just want to make a simple breakout game for practice. Lol.

Thanks, I think that helped. The formula doesn’t seem to work yet, but I’ll work on that. It changes the velocity some, but not much. If anyone has any ideas I’d appreciate it.

Got it!

Instead of just multiplying the xVel by the percent from the center of the paddle, I set it to the percent times 10, which is what I decided on for a max xVel for now. Works great.

Here’s the code I went with for the paddle bounce:

``````if (otherObject.tag == "Paddle")
{
ballDist = transform.position.x - otherObject.transform.position.x;

xVel = percentDist * 10;
zVel *= -1;
}
``````

Okay, so just in case anyone else looks for this, thought I’d share further updates. The way I had it before, I was getting random velocities for both x and z and only x changed when I bounced off the paddle. That means it was moving a lot quicker to the sides than when it was mostly vertical, so here’s what I did:

``````//This is the initial ball launch
if (Input.GetMouseButtonDown(0))
{

xVel = Random.Range(-maxSpeed + 5, maxSpeed - 5); //Set the initial x speed to be in a range set by a variable
if (xVel == 0)
xVel += 1; //Don't want the velocity to be 0, or it can potentially get stuck.
zVel = maxSpeed - Mathf.Abs(xVel); //Set the initial z speed to be whatever was left in the max speed
moving = true;
}

//Here is what happens when it hits the paddle