Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I have a problem with trying to make bullets bounce of walls... it involves orientation in 3D space?

Asked by
8391ice 91
7 years ago
Edited 7 years ago

I am making a game that involves shooting bullets that bounce off of walls. I've successfully managed to make a script that bounces the bullets off of walls, but there is one teensy problem: sometimes, the bullets bounce off in the wrong direction, and this depends on the direction in which I aim the bullets.

Below is a part of my script that makes it bounce off the walls. In it, I will outline why it is bouncing off in the wrong direction.

                    angle = 180-(2*math.deg(theta)) --This angle is calculated from an earlier part of the script; it is simple the angle that the bullet bounces off at. No need to really worry about this.
                    if horizontal == true then --Also earlier in the script, it is determined whether or not the wall that it bounced on is horizontal. If it is true, then it will focus on the X values, and if it is false, it will focus on the Z values.
                        if a.X < b.X then --THIS IS WHERE THE TROUBLE LIES! I reasoned that if the bullet's starting position's X is less than the impact position's X, it would imply that the bullet is coming in from the left, therefore it should head towards the right.
                            bullet.CFrame = bullet.CFrame*(CFrame.fromEulerAnglesXYZ(0, math.rad(angle), 0))
                        elseif a.X > b.X then --Vice versa; if the starting position's X is greater than the impact position's X, that means the bullet is coming in from the right, which means it should bounce off towards the left.
                            bullet.CFrame = bullet.CFrame*(CFrame.fromEulerAnglesXYZ(0, -(math.rad(angle)), 0))
                        end
                    else
                        if a.Z < b.Z then --Simply doing the same thing for the Z values.
                            bullet.CFrame = bullet.CFrame*(CFrame.fromEulerAnglesXYZ(0, math.rad(angle), 0))
                        elseif a.Z > b.Z then
                            bullet.CFrame = bullet.CFrame*(CFrame.fromEulerAnglesXYZ(0, -(math.rad(angle)), 0))
                        end
                    end
                    bullet.Speed.Velocity = 20*(bullet.CFrame.lookVector.Unit) --Setting the new direction that the bullet will travel in; "speed" is a BodyVelocity.
--The reason that sometimes the bullet bounces in the WRONG DIRECTION is, again, because of how the player is sometimes shooting. If the bullet is heading in the direction of the X plane, then the bullet will bounce correctly, because FROM THIS PERSPECTIVE, negative values are on the left and positive values are on the right. However, if the bullet was bouncing AWAY form the X plane, the negative values would be on the RIGHT and the positive values would be on the LEFT. This is what causes it to bounce off in the wrong direction.
--Depending on which direction the bullet is facing, it can bounce in the incorrect direction. But I have no clue as to how I can make it so that the bullet can recognize that the negative values are on the right instead of the left.

As I explained in the script, the bullet will bounce in the right direction if it is facing the X or Z plane, but if it is facing away from either, it'll bounce in the opposite direction. The way to fix this would be by scripting a way for the bullet to know what direction it is facing and register how the values are oriented so that it can bounce in the right direction, but I have no clue how I can do this. I've searched the ROBLOX wiki for something related to CFrame.lookVector or Rotation, but I just couldn't find anything that could help accomplish this. Do any of you have an idea of how I can make the bullet recognize the direction it is facing so it can bounce in the right direction or if there is a better way to go about this?

Answer this question