ping_pong/Assets/Scripts/BallBounces.cs
2025-03-30 20:12:08 +05:30

48 lines
1.4 KiB
C#

using UnityEngine;
using UnityEngine.UIElements;
public class BallBounces : MonoBehaviour
{
public BallMovement ballMovement;
public ScoreManager scoreManager;
private void Bounce(Collision2D collision)
{
Vector3 ballPosition = transform.position;
Vector3 racketPosition = collision.transform.position;
float racketHeight = collision.collider.bounds.size.y;
float positionX;
if(collision.gameObject.name == "Player_1")
{
positionX = 1;
}else
{
positionX = -1;
}
float positionY = (ballPosition.y - racketPosition.y) / racketHeight;
ballMovement.IncreaselHitcounter();
ballMovement.MoveBall(new Vector2(positionX, positionY));
}
private void OnCollisionEnter2D(Collision2D other)
{
if ( other.gameObject.name == "Player_1" || other.gameObject.name == "Player_2")
{
Bounce(other);
}
if (other.gameObject.name == "Right_Border")
{
scoreManager.Player_1Goal();
ballMovement.Player_1Start = false;
StartCoroutine(ballMovement.Launch());
}else if (other.gameObject.name.Contains("Left"))
{
scoreManager.Player_2Goal();
ballMovement.Player_1Start= true;
StartCoroutine(ballMovement.Launch());
}
}
}