- Home /
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.
Answer by Razputin · Jun 07, 2014 at 08:43 PM
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.
@Razputin I've never used the raycast, it seems very complicated, there is just no other way?
Raycast is simple, you have a starting point and a direction. Starting point is the bullet position, direction is its ....direction (?). Then you pass extra info if you need a distance or some layer but in your case the only extra is the RaycastHit to store some info on the hit.
Answer by EtoadEzra · Sep 03, 2017 at 10:50 PM
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.