- Home /
Can't stop shooting myself
I am trying to get a slowing moving bullet fired by me to not hit myself and go off some random direction. The bullet has a rigidbody with a box collider, and its suppose to reflect off of walls. My gun is near the center of the character and the bullet spawner is sometimes inside the characters collider. When I fire the bullet sometimes reflects off the characters collider and goes else where. How do I get it so that I don't hit from the inside?
I tried Physics.ignorecollision, layers, and a timer. Anything I'm doing wrong?
Answer by getyour411 · Mar 14, 2014 at 03:20 AM
Doesn't sound like you are doing anything wrong.
Create an empty GameObject on your Player just outside the radius of the characterController, positioned where you want the bullet to instantiate, fire/test that
for getyour411. Parent an empty object to where you want the bullet to instantiate and have it spawn there.
Well, I have a character controller and a box collider on the parent gameobject, and the bullet keeps on hitting inside the box collider where it is instantiated and reflects off of that. I had the bullet to ONLY reflect when it hits a collider with the tag "Wall", but it somehow manages to reflects off the "Player" tag when it instantiated.
if (hit.collider.tag == "Wall") {
rigidbody.velocity = Vector3.Reflect(oldVelocity, hit.contacts[0].normal);
}
I believe if you have gravity enabled on your bullet it will bounce off any colliders regardless of tag unless you ignore the collision. Not 100% on that but pretty sure.
So you probably need to specifically ignore collisions with the box collider, and the character controller since that has a capsule collider I believe.
Answer by RyanZimmerman87 · Mar 14, 2014 at 03:54 AM
The problem with placing the bullet outside of the parent object like the others suggested is that you could potentially not be able to shoot enemies when they are very close. Maybe that's not so much an issue with collision projectiles but I've had issues like that when using a Physics.RayCast to detect the object being hit.
Physics.IgnoreCollision will work perfectly if you set it up right. My guess is that you aren't ignoring collision for the actual bullet you are shooting. You will need to specifically ignore collision for each new bullet you create. Maybe you only ignored collision for like the first bullet or something?
This code seemed to work fine on my first project which was an FPS zombie game:
public Rigidbody bullet;
void playerBulletFunction()
{
//create a new bullet called instantiateBullet
Rigidbody instantiateBullet;
//you do this by cloning the public bullet variable from your script
//make a prefab of your bullet
//drag prefab into the "bullet" variable in Unity Editor
instantiateBullet = (Rigidbody)Instantiate(bullet, transform.position, Quaternion.identity);
instantiateBullet.AddForce(transform.forward *bulletVelocity);
Physics.IgnoreCollision(instantiateBullet.collider, player.collider);
}
Ugh, sorry for formatting whenever I copy an example from an actual script seems to get messed up beyond repair.
So some of this might be excessive and you already know how to do it. But it sounds to me like you made a simple mistake with the bullet ignoring collision and you are very close.
Also make sure there is only one collider for the player or you would have to do more Physics.IgnoreCollision()
Not sure why I used a Rigidbody for the bullet and instantiateBullet instead of gameObjects which seem more useful, but it's an easy example to post from my first project. This was one of my first bugs I had to solve. Hope that helps.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140210im_/https://answers.unity.com/themes/thub/images/avi.jpg)