I guess there is a bug when ball collides with top or bottom wall so sometimes it starts rolling on that wall from one side to another rather than reflecting…Can anyone help me with that what shall i do ??
Hi,
Welcome to our community!
I which course and lecture are you?
Ahh well its not a part of course actually I have taken Unity 2D course before and even I asked Q & A in Udemy hope you remember Prasoon Gupta…So just trying other game to make some hands on practice of 2D concepts…Hope you can help me with it… Sometimes it work then not idk why ?
Welcome to the forums.
If you need help, Post your code and what you have tried before. To be honest we need more information on what is happening, How the collision is set up, your code.
People will try to help but you need to give us more to work with or it is just a guessing game.
Thats my Ball Controller Script …
using System.Collections;
using UnityEngine;
public class BallController : MonoBehaviour
{
[SerializeField] private float ballSpeed = 20f;
[SerializeField] private float minSpeed = 15f;
[SerializeField] private float maxSpeed = 30f;
[SerializeField] private float minVerticalSpeed = 5f;
private Rigidbody2D rb;
private AudioSource audioSource;
void Start()
{
rb = GetComponent<Rigidbody2D>();
if (rb == null)
{
Debug.LogError("Rigidbody2D component is missing on the Ball GameObject!");
}
audioSource = GetComponent<AudioSource>();
StartCoroutine(StartBallMovement());
}
void FixedUpdate()
{
if (rb != null)
{
MaintainSpeed();
}
}
private void MaintainSpeed()
{
float currentSpeed = rb.velocity.magnitude;
if (currentSpeed < minSpeed)
{
rb.velocity = rb.velocity.normalized * minSpeed;
}
else if (currentSpeed > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
}
private void OnCollisionEnter2D(Collision2D colInfo)
{
if (colInfo.collider.CompareTag("TopCollider") || colInfo.collider.CompareTag("MiddleCollider") || colInfo.collider.CompareTag("BottomCollider"))
{
HandlePlayerCollision(colInfo.collider.tag);
PlayCollisionSound();
}
else if (colInfo.collider.CompareTag("TopWall") || colInfo.collider.CompareTag("BottomWall"))
{
HandleWallCollision(colInfo.collider.tag);
}
else if (colInfo.collider.CompareTag("ResetWall"))
{
ResetBall();
}
}
private void HandlePlayerCollision(string colliderTag)
{
Vector2 newVelocity = rb.velocity;
if (colliderTag == "TopCollider")
{
newVelocity.y = Mathf.Abs(rb.velocity.y);
if (Mathf.Abs(newVelocity.y) < minVerticalSpeed)
{
newVelocity.y = minVerticalSpeed;
}
}
else if (colliderTag == "MiddleCollider")
{
newVelocity.x = -rb.velocity.x;
}
else if (colliderTag == "BottomCollider")
{
newVelocity.y = -Mathf.Abs(rb.velocity.y);
if (Mathf.Abs(newVelocity.y) < minVerticalSpeed)
{
newVelocity.y = -minVerticalSpeed;
}
}
newVelocity.x = Mathf.Sign(newVelocity.x) * Mathf.Clamp(Mathf.Abs(newVelocity.x), minSpeed, maxSpeed);
newVelocity.y = Mathf.Clamp(newVelocity.y, -maxSpeed, maxSpeed);
rb.velocity = newVelocity;
}
private void HandleWallCollision(string colliderTag)
{
Vector2 newVelocity = rb.velocity;
if (colliderTag == "ReflectWall" || colliderTag == "ReflectWall")
{
newVelocity.y = -rb.velocity.y;
}
newVelocity.x = Mathf.Clamp(newVelocity.x, -maxSpeed, maxSpeed);
newVelocity.y = Mathf.Clamp(newVelocity.y, -maxSpeed, maxSpeed);
rb.velocity = newVelocity;
}
private void PlayCollisionSound()
{
if (audioSource != null && audioSource.clip != null)
{
audioSource.pitch = Random.Range(0.8f, 1.2f);
audioSource.Play();
}
else
{
Debug.LogWarning("AudioSource or AudioClip is missing!");
}
}
private void LaunchBall()
{
float randomDirection = Random.Range(0, 2) == 0 ? 1f : -1f;
rb.velocity = new Vector2(ballSpeed * randomDirection, Random.Range(-10f, 10f));
}
private void ResetBall()
{
StartCoroutine(ResetBallCoroutine());
}
private IEnumerator StartBallMovement()
{
yield return new WaitForSeconds(2);
LaunchBall();
}
private IEnumerator ResetBallCoroutine()
{
rb.velocity = Vector2.zero;
transform.position = Vector2.zero;
yield return new WaitForSeconds(0.5f);
LaunchBall();
}
}
Even I tried this wall reflector:
using UnityEngine;
public class WallReflector : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision)
{
Rigidbody2D rb = collision.gameObject.GetComponent();
if (rb != null && collision.gameObject.CompareTag(“Ball”))
{
ReflectBall(rb);
}
}
private void ReflectBall(Rigidbody2D rb)
{
// Reverse the ball's vertical velocity
rb.velocity = new Vector2(rb.velocity.x, -rb.velocity.y);
}
}
but its of no use i guess nothing changed
Can you show us a screen shot of the inspector for one of the walls that are not working?
Nah I just tried a trigger onto those walls and it worked like now… now i wanted to add some type of moving effect like neon light moving all around at a particular speed throughout game…but didnt have clue of how…saw tutorials all doing in previous version unable to figure out tried post process it does whole scene the tried shader and material one so inside it came and looks so weird …so still figuring out
Try the particle system. It is made for stuff like that.
Tried Particle System Particles coming though but how it should be gonna move from one side to another and the effect you know like of a spot light …not coming that way through aniation i thought to do moving part but not even lighting is coming though now… kinda feels stuckeddd
Do you have the 3d unity beginner course? In the Zombie Runner section it shows how to add a flashlight effect. Maybe you could adapt that to fit what you want.