- Home /
how to improve the the colliding system
hi i have a enemy and a script every think is working but my bullets donot collide with the enemy because they are to fast is there a another solution with out making the bullet slower
Have you tried different changing Rigidbody.collisionDetection$$anonymous$$ode
to Continuous[Dynamic/Speculative]
? The default, Discrete
, is higher performance, but fast moving objects can pass through other objects without detecting collisions, which seems to be what you're experiencing. You can read more about it here in the official docs: https://docs.unity3d.com/ScriptReference/Rigidbody-collisionDetection$$anonymous$$ode.html Hope this helps!
Answer by unity_ek98vnTRplGj8Q · May 28, 2020 at 03:29 PM
A very common solution for bullet collision is to use raycasting instead of a collider
void Update(){
bulletTravel = bulletSpeed * bulletDirection * Time.deltaTime;
RaycastHit hit;
//Cast a ray in the direction we are going to move this frame and see if we hit anything
if(Physics.Raycast(transform.position, bulletTravel, out hit, bulletTravel.magnitude)){
//We hit something, do bullet hit logic here
hit.gameObject.GetComponent<Health>().TakeDamage();
Destroy(gameObject);
} else{
//We didn't hit something, move the bullet normally
transform.position += bulletTravel;
}
}
thank you but do you know who to to make the shotgun spread with raycast because that is the reason why i used collision over raycast
This will depend on your game, for example if you have a shotgun that shoots 1000 pellets per shot then I would probably find a smarter solution than raycasting, but if you only have 10-20 pellets per shot then you can just do a raycast for each one. You don't even need to change the code I gave you, just attach the above script to your bullet prefab and spawn all the bullets in your shotgun spread, then each bullet will handle its own raycast, even if there are lots of bullets.
If you do think that this is going to be a performance issue then you will have to find a way to 'fake' collision detection (moving to colliders will not be any more performant, in fact it would possibly be worse than raycasting)
Answer by Map-Builder · May 28, 2020 at 11:20 PM
since shots are fast, maybe few raycast on shoot only (and less damage if far) with a delay to take damage based on distance and speed
Your answer
Follow this Question
Related Questions
How to create a collider-trigger that will start an animation when you press E 0 Answers
Possible to stop collider from "popping" through plane when re-centering? 2 Answers
Trigger Animation doesn't work, please help me. 1 Answer
Multiple colliders on one object? Or changing variables on a specific clone. 1 Answer
Collision for Animating Meshes 1 Answer