Question by
patrick1306 · Jul 02, 2020 at 12:32 PM ·
unity 5physicspong
Problem with Pong Physics
Hi,
So I am trying to make a Pong in 3D. The issue I have is that after a while (a couple of volleys usually) the ball doesn't bounce off the enemy's paddle anymore and seems to just stick to it and follows it along the Z axes.
The ball has a physics material with bounciness of 1 and bounces off everything else (walls and player's paddle) just fine.
Here's the code for the enemy's AI:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
Vector3 ballPosition;
Vector3 paddlePosition;
float movementSpeed = 0.1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
ballPosition = GameObject.Find("Ball").transform.position;
paddlePosition = this.transform.position;
if(paddlePosition.z < ballPosition.z)
{
paddlePosition.z += movementSpeed;
}
else if (paddlePosition.z > ballPosition.z)
{
paddlePosition.z -= movementSpeed;
}
transform.position = paddlePosition;
}
}
I am not sure what is causing this issue or how to solve it. Any suggestion is welcome.
Thanks!
Comment
Answer by patrick1306 · Jul 02, 2020 at 01:16 PM
In case anybody has the same problem I figured it out. I just added an impulse on the x axes every time the ball touches a paddle. Might not be the most efficient way to go about it but it solved the problem...