- Home /
perfect bounce when a bullet hits a wall
I wanna create bullets at runtime with physical features as the following code. As long as the speed increases to a higher enough number, such as 350.0f, 400.0f, 1000.0f etc, the bullet will pass through the surrounding walls without any bounce and any collision, which is not what I want.
Is there a way to set perfect bounce but the speed regardless ? (the Bounce Threshold is 0, Friction is 0, Minimum Friction Combine and Maximum Bounce Combine)
thanks ~
// bullet gameobject's physics settings
bullet.rigidbody.useGravity = false;
bullet.rigidbody.isKinematic = false;
bullet.rigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
Vector3 direction = (target.transform.position - bullet.transform.position)
Vector3 directionWithSpeed = changeSpeed(direction);
// to fire a bullet with a force here
bullet.rigidbody.AddForce(directionWithSpeed)
public float speed = 100f;
Vector3 changeSpeed(Vector3 src){
// the problem : there should be a limit on speed
speed +=10.0f;
return new Vector3(src.x*speed,0,src.z*speed)
}
This question comes up frequently. There is no one solution. The two ideas that come up as most helpful seem to be:
Reducing the Fixed Timestep. Go to Edit/Project Settings/Time and change the timestep from to 0.02 to something smaller like 0.01.
What kind of a bullet is it? For a rifle or something real like that using a Raycast ins$$anonymous$$d of a rigidbody is much safer.
the bullet is like a pin ball, running around inside a 2d box. it might hit the player(another ball) or the walls, so I give it rigidbody feature without gravity and friction
Answer by DanFrias · Jul 19, 2013 at 03:43 AM
For the bullet you want to use ContinuousDynamic mode, rather than Continuous, and for those objects with which it will collide (such as the wall), use Continuous mode for those.
I've tested both mode. The result is almost the same. That is, they all run into the wall when speed raises.