I’m trying to make a game similar to Tank Trouble where if the player shoots a bullet, it will bounce off the wall, just like in this image:
I achieved this with the following code:
float speed = 3.8f;
void Awake()
{
this.gameObject.GetComponent<Rigidbody>().velocity = transform.forward * speed;
}
private void Update()
{
this.gameObject.GetComponent<Rigidbody>().velocity = transform.forward * speed;
}
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "Wall")
{
print("coll norm: " + collision.GetContact(0).normal);
Vector3 v = Vector3.Reflect(bullet.transform.forward, collision.GetContact(0).normal);
bullet.transform.rotation = Quaternion.FromToRotation(Vector3.forward, v);
}
}
However sometimes when I shoot 5 bullets for example, some of them will take a different path after a bounce than others. The problem is obviously is with the way I make them bounce, but then I don’t see why they bounce in different ways. I should probably mention that the balls can not collide with eachother because their layer collision is disabled.