- Home /
Breakout-style paddle control problem
Hello. I am working on a Breakout-style Unity game and I am having an issue the interaction between the ball and the paddle. I want to make it so that depending on where the ball hits the edges of the paddle it will angle out more along the x axis. The way my code is currently working the balle always keeps the same angle no matter where it hits the paddle. Here is my code for the ball bouncing off the paddle. Thanks for any help!
if (collider.gameObject.name == "Paddle")
{
float x = hitFactor(transform.position, collider.transform.position, collider.collider.bounds.size.x);
Vector2 dir = new Vector2(x, 1).normalized;
GetComponent<Rigidbody2D>().velocity = dir * ballSpeed * Time.deltaTime;
}
private float hitFactor(Vector2 ballPos, Vector2 paddlePos, float paddleWidth)
{
return (ballPos.x - paddlePos.x) / paddleWidth;
}
Answer by sean244 · Jan 05, 2019 at 08:20 PM
This is how I did it in my own breakout clone and it seemed to do the trick.
[UPDATE] I've replaced the coroutine with an invoke statement, and replaced the 'if statements' inside OnCollisionEnter2D with a cleaner switch statement. The code still runs as expected.
using UnityEngine;
public class Ball : MonoBehaviour
{
[SerializeField]
private float _speed;
private Rigidbody2D _rb;
private Vector2 _direction;
private void Start()
{
_rb = GetComponent<Rigidbody2D>();
Invoke("DropBall", 1);
}
private void DropBall()
{
int random = Random.Range(-1, 2);
_direction = new Vector2(random, -1).normalized;
}
private void FixedUpdate()
{
_rb.velocity = _direction * _speed;
}
private void OnCollisionEnter2D(Collision2D other)
{
switch (other.gameObject.tag)
{
case "Paddle":
{
float x = HitFactor(transform.position, other.transform.position, other.collider.bounds.size.x);
_direction = new Vector2(x, 1).normalized;
break;
}
case "SideWall":
{
_direction.x *= -1;
break;
}
case "Ceiling":
{
_direction.y *= -1;
break;
}
}
}
private float HitFactor(Vector2 ballPos, Vector2 paddlePos, float paddleWidth)
{
return (ballPos.x - paddlePos.x) / paddleWidth;
}
}
Thanks for the reply. I created a script based on what you used, and now whenever the ball hits the paddle it teleports to position 0,0, and launches from there.
public class Ball : $$anonymous$$onoBehaviour
{
[SerializeField]
private float _speed;
private Rigidbody2D _rb;
private Vector2 _direction;
void Start()
{
_rb = GetComponent<Rigidbody2D>();
StartCoroutine(DropBall());
}
IEnumerator DropBall()
{
yield return new WaitForSeconds(1f);
float random = Random.Range(-1.0f, 2.0f);
_direction = new Vector2(random, -1).normalized;
}
private void FixedUpdate()
{
_rb.velocity = _direction * _speed;
}
private void OnCollisionEnter2D(Collision2D other)
{
Vector2 normal = other.contacts[0].normal;
if (other.gameObject.name == "Paddle")
{
float x = HitFactor(transform.position, other.transform.position, other.collider.bounds.size.x);
_direction = new Vector2(x, 1).normalized;
}
if (other.gameObject.tag == "SideWall")
{
_direction = Vector2.Reflect(_direction, normal);
_direction.Normalize();
}
if (other.gameObject.name == "Ceiling")
{
_direction = Vector2.Reflect(_direction, normal);
_direction.Normalize();
}
}
float HitFactor(Vector2 ballPos, Vector2 paddlePos, float paddleWidth)
{
return (ballPos.x - paddlePos.x) / paddleWidth;
}
}