- Home /
Ricochet Problem of projectile bullets
Hi everyone, I'm having trouble implementing bullets that ricochet a certain amount of times.
PROBLEM
1) The direction of the reflection is correct, but for some reason, it reflects in a wrong direction briefly before changing to the correct reflection direction. (I do notice if I lower the bullet speed it will reflect correctly, so it has something to do with the line casting, but I can't wrap my head on the specific problem)
2) The reflection only occurs once, despite the reflections variable being at 100. I have checked and the Line casting does collide with the second wall, but the reflection does not occur.
3) It's not in the video, but sometimes my projectiles pass through walls. Is this a problem of the Linecast not being able to detect it? I thought Line cast was supposed to overcome this problem.
Here is a video that indicates the problems above.
My friend really wants to have trails for the bullets, so projectiles + trail renderer is what I'm using.
I did think of alternatives using Raycasts to detect the richochet points and for collision, and have my projectile bullets follow the raycast path until it reaches the hit.point, then reflecting it. (Just for the trails).
But then again it's the same thing because the bullets will be too fast to even land on that hit point.
This is the script attached to the bullet.
void FixedUpdate () {
RaycastHit hit;
if (Physics.Linecast (previousPosition, this.transform.position, out hit)) {
if (hit.transform.gameObject.tag == "Target") {
if (reflections == 0) {
return;
}
Vector3 newDirection = Vector3.Reflect (this.transform.forward, hit.normal);
this.transform.position = hit.point;
rb.velocity = newDirection * bulletSpeed;
reflections--;
}
}
previousPosition = this.transform.position;
}
This sounds like a problem with physics simulation, as if the position of the projectile were corrected after the collision. And sure enough, that's exactly what the code does: you modify the position to be the hit point, and then modify the velocity of the rigidbody.
Both of these operations are recommended to be avoided, because they are inefficient and create unrealistic behaviour (see documentation). If you really need to modify the position of a rigidbody, use rigidbody.position
(for some reason it is 10-20 times faster).
Also, your projectile is fast enough to penetrate into colliders (that's why it doesn't miss objects when you lower its speed). Setting the rigidbody's Collision detection property to Continuous should help with this.
Of course, none of this solves your actual problem. I would just try to use the normal physics engine to reflect the projectile, after setting a 100% bouncy and 0 friction physics material, and drag set to 0 and gravity disabled. Have you tried this?
And if this doesn't work, you can also try manually simulating with raycasts and interpolation, and setting the rigidbody to kinematic.
Your answer
Follow this Question
Related Questions
Multiple linecasts question 1 Answer
Multiple raycasts in same fixedupdate 3 Answers
Linecast blocked by colliders not actually in the way? 0 Answers
Help with Raycast C# 0 Answers