- Home /
My ball in pong passes through the paddles.
I'm trying to make a pong duplicate and the ball keeps passing through the paddles. It's slow enough to see go through the paddle. Here is the Ball Movement Script.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BallMovement : MonoBehaviour {
public float speed = 10;
private Rigidbody2D rigidBody;
//private AudioSource audiosource;
void Start () {
rigidBody = GetComponent<Rigidbody2D>();
rigidBody.velocity = Vector2.right * speed;
}
void OnCollisionEnter2D(Collision2D col)
{
if ((col.gameObject.name == "Left Paddle") ||
(col.gameObject.name == "Right Paddle"))
{
HandlePaddleHit(col);
}
if ((col.gameObject.name == "Wall Top") ||
(col.gameObject.name == "Wall Bottom"))
{
//SoundManager.Instance.PlayOneShot
//(SoundManager.Instance.wallBloop)
}
if ((col.gameObject.name == "Right Goal") ||
(col.gameObject.name == "Left Goal"))
{
//SoundManager.Instance.PlayOneShot
//(SoundManager.Instance.goalBloop)
transform.position = new Vector2(0, 0);
}
}
void HandlePaddleHit(Collision2D col)
{
float y = BallHitPaddleWhere(transform.position,
col.transform.position,
col.collider.bounds.size.y);
Vector2 dir = new Vector2();
if(col.gameObject.name == "LeftPaddle")
{
dir = new Vector2(1, y).normalized;
}
if (col.gameObject.name == "RightPaddle")
{
dir = new Vector2(-1, y).normalized;
}
rigidBody.velocity = dir * speed;
//SoundManager.Instance.PlayOneShot
//(SoundManager.Instance.hitPaddleBloop)
}
float BallHitPaddleWhere(Vector2 ball, Vector2 paddle, float paddleHeight)
{
return (ball.y - paddle.y) / paddleHeight;
}
}
If able, please tell me anything wrong with the code.
Answer by ChristosKapenis · Jul 03, 2017 at 07:49 PM
Make sure that you have a collider attached on your object and if that's not the case try changing your Rigidbody's collision detection to "Continuous" or "Continuous Dynamic":
Answer by TheDJBuntin · Jul 03, 2017 at 07:54 PM
From a similar question I answered:
Have you tried doing it with a lower velocity to make sure it works?
Remember that Update() is ran every FRAME. If there is no frames where the ball is touching the collider there will be no collision. ie. Frame 500 ball is at 500, 0. Target is at 700, 0. But Ball is traveling at 300units per frame. The next time update is called, ball will be at 800, 0.
To solve this, use Continious Collision Detection instead of discrete collision detection. [Christos has provided a screenshot of where to change that.]
Although I've never used it, a quick google also pulled up DontGoThroughThings which uses raycasting to avoid the physics engine allowing fast-moving objects to go through other objects. Here is another thread about this.
Answer by Vollmondum · Jul 03, 2017 at 08:34 PM
Untick "Is Trigger" in colliders
One of colliding thingies (a ball or paddle) should have simple shape collider
Open your paddles in 3Ds Max and check Normals. Inverted normals collide from the inside
Check your script with standard Unity cubes
See is your colliders' size coinside with gameObjects sizes. Sometimes those are a dot.