Hey everyone
I’m currently making a fps game but I have a dilemma about the shooting. I know this has been asked many times but I want to be sure of the best way.
I really want a visual representation of a bullet to make the game look better but there is a problem with the collision detection because the bullet is too fast to detect a collision(I think that’s the reason).
Raycasting is the safest way and most common as for my understanding but there’s no visual representation of the bullet(I can use a Raycast only to know my bullets direction and destroy it by calculating the time it would have been destroyed by collision and that’s what I’ll probably do if there will be no other way).
I think I can also shoot a ray from the bullet while it’s flying and destroy it when it’s close to a colliders(by the distance from the bullet to the collider).
If there are better way I would really like to know, if not what do you guys think is the bast way??
Using Raycasts to determine where it hits is a common practice. in fact weapons in games that do this are typically classified as “Hitscan weapons” and it is by far the cheapest solution…
You can use fast bullets with projectiles but to do so, I believe you want to use a RigidBody with a Collision Mode set to ContinuousDynamic. it prevents fast objects from passing through without being detected, but it does cause the physics engine to do a little more math. so if profileing reveals the physics as a bottle neck be warned that you might need to do some optimizations with the colliders and the layers
First of all thanks for helping, second, won’t the Raycast way make it look less realistic? And can you also tell me what’s your suggestion?
Again, thanks alot!
If you want to have an actual flying projectile with accurate hit detection, add a raycast to the bullet. Make the ray long enough that it doesn’t go through objects pending on frame rate.
A starting point:
Transform t;
public float speed = 10;
public Transform rayPos;
public LayerMask layermask;
void Awake() {
t = transform;
}
void FixedUpdate() {
t.Translate (Vector3.forward * speed * Time.fixedDeltaTime);
RaycastHit hit;
Physics.Raycast (rayPos.position, rayPos.forward, out hit, 6, layermask);
Debug.DrawLine (rayPos.position, rayPos.position + rayPos.forward * 6, Color.green);
if (hit.transform) {
OnImpact (hit.transform, hit.point, hit.normal);
}
}
For OnImpact you just display your results. Passing variables: hit.transform is the object your hitting. hit.point is the position where it hit (for impacts), and hit.normal is where you would display a bullet hole decal. You don’t have to use these variables, but it is a good starting point.
So if I understand correctly the Raycast is starts from the bullet and forward and is used to detect when the bullet hit instead of collision right?
And thank again I’ll give it a try!
Something like this. Adjust the rayPos transform as needed to get the desired effect. The ray generally you want in front of the bullet. Otherwise the bullet will go through the target before the ray connects.