- Home /
Question by
driuki · Mar 06 at 03:47 PM ·
collisionrigidbody2d
Ball slows or stops after it reflects of object in tight space
Hi,
I have a bit of a problem. I'm making a Brick Braker game. Ball bounces correctly moust of the time, but when it gets in tight spaces ball stops or slows dramatically.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallController : MonoBehaviour {
public Rigidbody2D rb;
public float force;
float constantSpeed = 4.25f;
Vector3 lastVelocity;
public static BallController instance;
private void Awake() {
if(instance == null) {
instance = this;
}
}
// Start is called before the first frame update
void Start() {
}
void FixedUpdate() {
lastVelocity = constantSpeed * (rb.velocity.normalized);
}
// Update is called once per frame
void Update() {
}
void OnCollisionEnter2D(Collision2D coll) {
if(coll.gameObject.tag == "Brick" || coll.gameObject.tag == "Metal" || coll.gameObject.tag == "Wall") {
FindObjectOfType<AudioManager>().Play("Hit");
Vector3 direction = Vector3.Reflect(lastVelocity.normalized, coll.contacts[0].normal);
/*I tried with AddForce and without, but no help*/
rb.AddForce(direction, ForceMode2D.Impulse);
rb.velocity = direction * Mathf.Max(lastVelocity.magnitude, 0f);
}
if(coll.gameObject.tag == "Paddle") {
FindObjectOfType<AudioManager>().Play("Hit");
}
}
}
Also added physics material on the ball with friction = 0. So where I did a mistake and what I don't see?
Comment
Your answer
Follow this Question
Related Questions
How to constrain 2D objects? 2 Answers
How to freeze all enemy objects at once 1 Answer
Player can't move on a surface that is moving up fast. 0 Answers
How do I collide a Kinematic sprite ? 0 Answers
Incorrect velocity after collision 1 Answer