Bullet bounces off the wall

Hello to everyone

I wanted to make a script that makes the bullet bounced three times on the wall, this is what I tried to do.

private  Vector3 velocity; 

public float speed;

private int n = 0;
	
	
	void Awake()
	{  
		velocity = this.transform.forward;
		speed = 10;
	}
	
	
	void Update()
	{
		this.transform.position += velocity * Time.deltaTime * speed;
	}
	
	
	void OnCollisionEnter( Collision info) 
	{
		if ((info.gameObject.tag == "Muri") && (n<3))
		{
			n++;
			foreach(var contact in info.contacts )
			{
				velocity = Quaternion.AngleAxis(180, contact.normal) * transform.forward * -1;
			}
		}
		else 
			Destroy (gameObject);

	}

It works pretty but there are three problems:

1)Occasionally through the wall.

2)Occasionally does as in the pictures.


3)Occasionally increases the speed.

It is very likely that I’m completely wrong way, then you do another picture for you to understand what should happen.

I hope I was clear, and I apologize in advance if it’s a stupid question.

I assume you’re instantiating a circle for the bullet or some object. When stuff moves really fast in unity it can sometimes glitch through the wall. I don’t think theres any work around for this. My only suggestion would be to use raycasts instead, then maybe attach some particle to make it seem like theres a bullet flying around.

Sorry thats the only question I can answer out of your three, but I hope that at least helps a little.

for the glitching through walls, if you’re using a rigidbody, set the collision detection in the rigidbody to continuous as opposed to discrete. You can also set the Fixed time step under Edit>Project Settings> Time, to a smaller amount.