I'm new to Unity and C# (though I'm experienced in python) and I am trying to make a pong clone, I've spent ages googling and trying to fix this issue but for some reason my OnCollisionEnter2D won't work
My paddle hit method:
public void PaddleHit(Collision2D col)
{
float y = WhereBallHitPaddle(transform.position,
col.transform.position,
col.collider.bounds.size.y);
Vector2 dir = new Vector2();
//determing direction to left or right depending on the paddle
if (col.gameObject.name == "Player1")
{
dir = new Vector2(1, y).normalized;
}
if (col.gameObject.name == "Player2")
{
dir = new Vector2(-1, y).normalized;
}
rb.velocity = dir * speed;
//play ball paddle collision sound
SoundManager.Instance.PlayOneShot
(SoundManager.Instance.ballpaddle);
}
When I run the game the ball moves, then when it comes into contact with a paddle it just stops moving, and if it comes into contact with a wall it bounces off, but at no point does it trigger the OnCollisionEnter2D. (I think my codes indents aren't showing properly here but it is all indented correctly, there are no compile errors or anything).
OnCollisionEnter2D
function? Because I am seeing 2 copies of yourPaddleHit
function in this post... \$\endgroup\$ – WQYeo Aug 8 '18 at 10:19OnCollisionEnter2D
method? It needs to be named exactly that. \$\endgroup\$ – DMGregory♦ Aug 8 '18 at 18:36