- Home /
Bullets are phasing through my player character. How to make sure every bullet registers collision ?
This is the code I'm using to shoot bullets.
public Rigidbody projectile;
public float speed = 200f;
public float fireRate = 0.1f;
private float nextFire = 0.0f;
void Update() {
if (Time.time > nextFire) {
nextFire = Time.time + fireRate;
Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
}
}
Answer by srylain · Dec 22, 2015 at 07:29 PM
Your bullets are probably moving too fast, and on the first frame they are in front of the character and on the next they are on the other side.
A couple things you can do:
Make the bullets move slower, which is the easiest thing you could do. You probably don't want to do this because it will effect the speed of your game however.
Make the bullets (or just their collider) bigger.
Use a raycast from the bullet's position on the previous frame to its position on the current frame and use that to check if there was any collision.
Answer by Leuthil · Dec 22, 2015 at 07:29 PM
You can try setting the rigidbody Collision Detection setting of the bullet to Continuous Dynamic and the rigidbody Collision Detection setting of the player to Continuous. Alternatively if your bullets are moving so fast that they are basically instantaneous you can look into using Raycasts instead which would be a lot more efficient and effective.
Your answer
Follow this Question
Related Questions
Get an Object to Face/Move to RayCast Hit.point? 1 Answer
Internal collisions 1 Answer
Big problem with making a FPS game 1 Answer
Destroy-able won't destroy 1 Answer
Raycast shooting problems 1 Answer