using System;
using UnityEngine;
using Random = UnityEngine.Random;
public class BallManager : MonoBehaviour
{
private Rigidbody2D ballRigidBody;
private float collisionTime = 0f; // Time spent in collision
private bool isColliding = false; // Track if the ball is colliding with the wall
private bool isPlayerHitBall = false;
public float collisionThreshold = 5f; // Time threshold to apply force
Collision2D wallData;
// references
AnimationManager animationManager;
public float deflectionForce = 0.000005f;
AudioManager audioManager;
public float speedThreshold = 0.2f;
public float bounceDamping = 0.95f;
private Vector2 preCollisionVelocity;
private void Start()
{
audioManager = FindAnyObjectByType<AudioManager>();
ballRigidBody = GetComponent<Rigidbody2D>();
animationManager = FindAnyObjectByType<AnimationManager>();
}
private void Update()
{
// check if the ball is moving
CheckIfBallIsMoving();
// Check if we are in a collision and update the timer
// if (isColliding)
// {
// collisionTime += Time.deltaTime; // Count time during collision
// }
if (isPlayerHitBall)
{
if (wallData != null)
{
HandleBallDeflection(wallData);
}
else
{
Debug.Log("Some error occourred");
}
}
}
private void CheckIfBallIsMoving()
{
if (ballRigidBody.velocity.magnitude < 0.12f)
{
animationManager.StopBallAnimation();
}
else
{
animationManager.PlayBallAnimation();
}
}
// Called when the ball first makes contact with a wall
private void OnCollisionEnter2D(Collision2D other)
{
audioManager.PlayCollisionSound();
Handheld.Vibrate();
if (isColliding)
{
if (other.gameObject.tag == "PlayerA" || other.gameObject.tag == "PlayerB")
{
Debug.Log(other.gameObject.tag);
isPlayerHitBall = true;
}
}
if (other.gameObject.tag == "outerBox")
{
Vector2 duplyNormal = other.contacts[0].normal;
Vector2 reflectedVelocity = Vector2.Reflect(ballRigidBody.velocity, duplyNormal);
ballRigidBody.velocity = reflectedVelocity;
// Play collision sound
audioManager.PlayCollisionSound();
}
}
// Called as long as the ball is in contact with the wall
private void OnCollisionStay2D(Collision2D other)
{
if (other.gameObject.tag == "outerBox")
{
isColliding = true; // Ball is colliding
wallData = other;
// // If the ball has been colliding for more than the threshold time
// if (collisionTime >= collisionThreshold)
// {
// HandleBallDeflection(other);
// }
}
}
// Called when the ball exits the wall collision
private void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.tag == "outerBox")
{
isColliding = false; // Ball is no longer colliding
collisionTime = 0f; // Reset the collision timer
isPlayerHitBall = false;
}
}
// Function to handle the ball deflection logic
private void HandleBallDeflection(Collision2D other)
{
Vector2 contactNormal = other.contacts[0].normal;
// Apply force in the opposite direction of the wall to prevent sticking
ballRigidBody.AddForce(contactNormal * deflectionForce, ForceMode2D.Force);
}
}
Simply putting a title and dumping code isn’t how to get help here, you need to ask specific questions if you want specific answers.
Also, you need to highliight/show what you expect to happen versus what is actually happening. It’s not possible to just debug the code in your head and know what is happening.
Thanks.