- Home /
Balls bounce off wall in different ways
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.
Answer by mudassirrazi · Feb 03, 2020 at 04:20 PM
You don't need script for this.Add a physical material to the wall with 0 friction and Bounce 1.Should work.
If i do this, and move the balls with AddForce, the balls sometimes slide along the wall ins$$anonymous$$d of bouncing.
Your answer
Follow this Question
Related Questions
Reflecting Objects off of Boundaries/Walls 2 Answers
How to make ball bounce off screen boundaries? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Ping Pong Reflect 2 Answers