I’m currently creating a sort of Pong clone as practice, but I’ve run into some troubles.
I have some code separated into two different scripts in which when the ball hits a racket, the racket will gain one point. However, if the ball hits a colored wall, the racket on the same side of the wall looses a point.
However, I’ve noticed something very odd: Whenever I keep the rackets still, the scoring system works as expected: https://i.gyazo.com/7c9d9756f2a3bfe9f7a720296d382684.mp4
But if I begin moving a racket, and the ball is below said racket, that racket will gain a seemingly random amount of points without even touching the ball: https://i.gyazo.com/515a54bb35f24e614a797d3d2d90252a.mp4
I’ve checked the colliders in real-time on both the ball and the rackets, and nothing seems out of the ordinary. I just don’t understand what’s causing this issue.
Here’s the script attached to the ball GameObject:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Ball : MonoBehaviour {
public float speed = 30;
public Text timeText;
public AudioSource gainSound;
public AudioSource lossSound;
public AudioSource other;
// Use this for initialization
void Start ()
{
// Initial Velocity
GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
}
float hitFactor(Vector2 ballPos, Vector2 racketPos,
float racketHeight)
{
return (ballPos.y - racketPos.y) / racketHeight;
}
void Update()
{
if (timeText.text == "0")
{
timeText.text = "Game Over";
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "RacketLeft")
{
gainSound.Play();
// Calculate hit Factor
float y = hitFactor(transform.position,
col.transform.position,
col.collider.bounds.size.y);
// Calculate direction, make Length=1 via .normalized
Vector2 dir = new Vector2(1, y).normalized;
//Set Velocity with dir * speed
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
// Hit the right Racket?
if (col.gameObject.name == "RacketRight")
{
gainSound.Play();
// Calculate hit Factor
float y = hitFactor(transform.position,
col.transform.position,
col.collider.bounds.size.y);
// Calculate direction, make Length=1 via .normalized
Vector2 dir = new Vector2(-1, y).normalized;
// Set Velocity with dir * speed
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
if (col.gameObject.tag == "Loss")
{
lossSound.Play();
}
if (col.gameObject.tag == "Other")
{
other.Play();
}
}
}
Here’s the script attached to the rackets:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MoveRacket : MonoBehaviour {
public float speed = 30;
public string axis = "Vertical";
public Text score;
private float scoreAmount = 0;
void FixedUpdate()
{
float v = Input.GetAxisRaw(axis);
GetComponent<Rigidbody2D>().velocity = new Vector3(0, v) * speed;
}
private void OnCollisionEnter2D(Collision2D collision)
{
scoreAmount = scoreAmount + 1;
score.text = scoreAmount.ToString();
}
}
I understand that this problem may or may not be too involved (I’m a beginner so I’m not sure what would qualify as complex), but any help at all would be greatly appreciated!
Edit: Forgot to include that this is a two player game, with one player controlling the racket with the arrow keys and another player controlling the other racket with WASD.