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

61 lines
1.3 KiB
C#

using System.Collections;
using UnityEngine;
public class BallMovement : MonoBehaviour
{
public float StartSpeed;
public float extraSpeed;
public float maxExtraSpeed;
public bool Player_1Start = true;
private int hitCounter = 0;
private Rigidbody2D rb;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
rb = GetComponent<Rigidbody2D>();
StartCoroutine(Launch());
}
private void RestartBall()
{
rb.linearVelocity = new Vector2(0,0);
transform.position = new Vector2(0,0);
}
public IEnumerator Launch()
{
RestartBall();
hitCounter = 1;
yield return new WaitForSeconds(1);
if (Player_1Start == true)
{
MoveBall(new Vector2(-1 ,0));
}
else
{
MoveBall(new Vector2(1,0));
}
}
public void MoveBall(Vector2 direction)
{
direction = direction.normalized;
float ballSpeed = StartSpeed * hitCounter * extraSpeed;
rb.linearVelocity = direction * ballSpeed;
}
public void IncreaselHitcounter()
{
if ( hitCounter * extraSpeed < maxExtraSpeed)
{
hitCounter ++;
}
}
}