TL;DR
How do I make a ball that has no acceleration from outside forces, but can also handle collisions well enough to not pass through walls?
I ran into a simple problem the other day, but the more I try to figure it out, the more it seems like there is no simple solution… I am currently making an atari break out clone, and I want to make a ball that bounces off of walls and also other rigid bodies that happen to be in the scene, such as creatures and people. The ball was a rigid body, I removed the gravity, and I added a material with no friction and a bounciness of 1. the ball did just fine and kept up a constant speed, but when it collided with other objects that were moving in the scene it slowed down… Eventually it slowed and slowed until the game was not playable.
I made a thread about this in the 2D forum. A man of the name RockyWallbanger came along and told me that it was not possible to do such a thing with a dynamic body.
He gave me this script that i attached to my ball;
[RequireComponent(typeof(Rigidbody2D))]
public class ConstantVelocity : MonoBehaviour
{
//These are just to give the rigidbody an initial velocity
private float randomX;
private float randomY;
private Rigidbody2D rb;
private void Awake()
{
//Randomize the initial velocity
randomX = Random.Range(1f, 5f);
randomY = Random.Range(1f, 5f);
//Get a reference to our Rigidbody2D component, set it's body type to Kinematic
//UseFullKinematicContacts means this will still register collisions with the Physics2D system
rb = GetComponent<Rigidbody2D>();
rb.isKinematic = true;
rb.useFullKinematicContacts = true;
//Set the initial velocity of the Rigidbody
rb.velocity = new Vector2(randomX, randomY);
}
private void OnCollisionEnter2D(Collision2D collision)
{
//When we register a collision, we're going to get the first point of collision
//Then we just reflect our rigidbody about the contact normal, maintaining velocity
ContactPoint2D hit = collision.GetContact(0);
rb.velocity = Vector2.Reflect(rb.velocity, hit.normal);
}
private void FixedUpdate()
{
//Logging the velocity magnitude will show us the speed not changing
//Note, you may see tiny tiny changes due to floating point precision
Debug.Log(rb.velocity.magnitude.ToString());
}
}
[/QUOTE]
This worked. The problem of the ball slowing down was solved… However, here comes my new problem, and the reason I am taking this to the physics forum. Because I am out of ideas, and I do not really think anyone wants to read the big long thread to solve a problem that arose, due to a solution, especially since its physics based in the 2D forum. (if you do, here is a link to it.)
Since I attached that script to the ball it bounces off of any collider it runs into, however when it hits corners, or where two colliders intersect and It hits two colliders at the same time it cannot register them both, and picks one of the colliders to register, and ignores the other such that it passes through that one.
I tried setting the fixed timeset to .01 and that helped it to register corners better, but still when it hits multiple colliders at the exact same time, it ignores all but one…
I am just not sure what to do It seems like such a simple thing; “make ball go and bounce off of things while remaining that speed.” but I have been absolutely pulling my hair out over this issue. Any input you have I would appreciate.
PS sorry if I make any rookie mistakes, I’m kindof new to unity, and i’m new to this forum, so let me know if I did something wrong or maybe broke some sort of rule about making too many threads about the same problem, if so, I am so sorry