- Home /
Bullet Collision Issues
Right now my bullets are just spheres with rigid bodies (continuous-dynamic) attached to them. In order to shoot them I do:
1.Create the projectile using Instantiate 2. In Projectile.Start() do:
this.GetComponent<Rigidbody>().AddForce(transform.forward * this.TrajectoryInformation.MuzzleVelocity * 500000 * Time.deltaTime);
This moves the bullet quickly (which I want), but it sends other objects flying which I do not want. Also, I attempt to apply a decal at the location of the hit like so:
Vector3 hitPosition = new Vector3 { x = collision.contacts[0].point.x, y = collision.contacts[0].point.y, z = collision.contacts[0].point.z };
Quaternion rotation = Quaternion.Euler(collision.contacts[0].normal);
//Create the decal (ie: bullet hole)
GameObject decal = (GameObject)Instantiate(DecalPrefab, hitPosition, rotation);
//Makes sure the decal stays on whatever it hit
decal.transform.parent = collision.collider.transform;
This sometimes creates a decal but the decal is never rotated properly.
Any ideas on how to fix these things?
Answer by Niklas · Mar 17, 2011 at 08:46 PM
You say your decal sometimes works? Maby the bullet is so fast that the collision detection dont work. Have you tried to extrapolate the rigidbody?
If the bullet is intended to fly fast, maby it is better to do the collision detection with a ray, place the decal at the collided position and just use the model of the bullet for visuals.
Exactly. Unless you NEED to see the bullet fly (like it's pretty slow, which yours is not), use Raycast ins$$anonymous$$d.
Thanks, I hadn't considered that rays would be a good idea for fast moving bullets. I don't suppose its feasible to also create a tracer/trail renderer? and what would the physics be like if I did this? Would I create a ray from the bullet start position (tip of gun) to the point where the camera is looking and then create some sort of explosion at the point of contact?
Well it depends entirely on what you want to do with this. You could just as well draw the line from the head as well since this will not be visible and it might feel better. You need to try and see which is best for your situation
Your answer
Follow this Question
Related Questions
Raycasr in my fps? 1 Answer
Help with getting a bullet to move. 2 Answers
Bullet does not move forward 1 Answer
Transform.position bullet won't align 1 Answer
How to get bullets to hit crosshair 2 Answers