I’ve been trying to add life counter, I came up with this code, it resets the ball to the paddle but the ball immediately falls through it causing to quickly lose 3 lifes and load “lose” scene. How do i stop the ball at the paddle? it’s probably easy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoseCollider : MonoBehaviour {
private bool GameStarted = false;
private LevelManager levelManager;
public Paddle paddle;
public Ball ball;
public Vector3 paddleToBallVector;
private int lifes=3;
void Start()
{
ball = GameObject.FindObjectOfType<Ball>();
paddle = GameObject.FindObjectOfType<Paddle>();
levelManager= GameObject.FindObjectOfType<LevelManager>();
}
void Update()
{
GameStarted = false;
}
void OnTriggerExit2D(Collider2D trigger)
{
lifes--;
if (!GameStarted)
{
ball.transform.position = paddleToBallVector + new Vector3(paddle.transform.position.x, 0f, 0f);
}
if (Input.GetMouseButtonDown(0) && (GameStarted != false))
{
GameStarted = true;
ball.GetComponent<Rigidbody2D>().velocity = new Vector2(Random.Range(-2f, 2f), 12f);
}
print(lifes);
if (lifes<=0)
{
levelManager.LoadLevel("Lose");
lifes = 3;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
print("Collision");
}
}