I am making a brick breaker game.
I have a ball prefab. And as the ball is colliding everywhere I want should be handling collision control through the ball, I have made an empty GameObject and have a script in it named as BallManager.
I need advice on that.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class BallManager : MonoBehaviour
{
Vector3 StartPos, EndPos;
public GameObject BallPrefab;
public GameObject DirectionLine;
public float Speed, Values;
Vector3 ScreenSize;
float X, Y;
GameObject G;
private void Start()
{
ScreenSize = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));
G = Instantiate(BallPrefab, transform.position, Quaternion.identity);
float GBoundsX = G.transform.GetComponent<SpriteRenderer>().bounds.size.x;
float GBoundsY = G.transform.GetComponent<SpriteRenderer>().bounds.size.y;
float PaddleBoundsX = PaddleManager.Instance.transform.GetComponent<SpriteRenderer>().bounds.size.x;
float PaddleBoundsY = PaddleManager.Instance.transform.GetComponent<SpriteRenderer>().bounds.size.y;
G.transform.position = PaddleManager.Instance.transform.position + new Vector3(0, (PaddleBoundsY / 2) + (GBoundsY / 2), 0);
}
private void Update()
{
if (PaddleManager.Instance.isBallReleased == false)
{
if (Input.GetMouseButtonDown(0))
{
StartPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
DirectionLine.SetActive(true);
}
if (Input.GetMouseButton(0))
{
EndPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
X = EndPos.x - StartPos.x;
Y = EndPos.y - StartPos.y;
//Debug.Log("X :: " + X);
//Debug.Log("Y :: " + Y);
float Angle = Mathf.Atan2(X, Y) * Mathf.Rad2Deg;
DirectionLine.transform.rotation = Quaternion.Euler(0, 0, -Angle);
}
if (Input.GetMouseButtonUp(0))
{
EndPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 Difference = EndPos - StartPos;
X = StartPos.x - EndPos.x;
Y = StartPos.y - EndPos.y;
Vector2 temp = new Vector2(X, Y).normalized * Values;
G.GetComponent<Rigidbody2D>().velocity = Difference.normalized * Speed * Values;
DirectionLine.gameObject.SetActive(false);
PaddleManager.Instance.isBallReleased = true;
}
}
}
}
I don't think I understand the question. Do you want to detect the collisions in the ball object but then handle the consequences of the collisions in a different object?
– dstearsI have a ball prefab, and a ballmanger empty object. ballmanager has script which is managing the ball launching.
– GauravRathod11