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

Why Does This Script for Bouncing a Bullet Off the Wall Completely Fail?

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

As the title implies, my game involves shooting bullets that bounce off of walls. Below is an excerpt from the script that is supposed to make it bounce; it is the code that runs once the bullet hits a wall. But when the bullet reaches the wall, it doesn't bounce; the Speed (the name of a BodyVelocity within the bullet that powers its movement) is deleted, causing the bullet to fall down and through the world. Why in the world is this?

            script.Parent.Speed.Velocity = Vector3.new(0, 0, 0) --Stop the bullet from traveling any further.
            if script.Touched.Horizontal.Value == true then --Calculating the angle that the bullet traveled at so that it can bounce off the wall at the same angle.
                c = {a.X, b.Z}
            else
                c = {b.X, a.Z}
            end
            disA = math.sqrt(((c[1]-a.X)^2)+((c[2]-a.Z)^2))
            disB = math.sqrt(((c[1]-b.X)^2)+((c[2]-b.Z)^2))
            theta = math.atan(disB/disA) --This math isn't really important, don't worry... Just think of theta as the angle that has been calculated (in radians).
            newDirection = Instance.new("Part", game.Workspace) --Making a new part that holds the direction that the bullet is supposed to face after bouncing off.
            newDirection.Name = "Vector"
            newDirection.Size = Vector3.new(.75, .75, .75)
            newDirection.Anchored = true
            newDirection.CanCollide = false
            newDirection.Position = Vector3.new(script.Parent.Position.X, script.Parent.Position.Y + 5, script.Parent.Position.Z) --Placing it 5 studs above the actual bullet.
            newDirection.Transparency = 1
            newDirection.Rotation = script.Parent.Rotation --Setting the rotation of the part to be the same as the bullet so that it can be rotated further with theta.
            if script.Touched.Horizontal.Value == true then
                if a.X < b.X then
                    newDirection.CFrame = newDirection.CFrame*CFrame.fromEulerAnglesXYZ(0, theta, 0)
                else
                    newDirection.CFrame = newDirection.CFrame*CFrame.fromEulerAnglesXYZ(0, -theta, 0) --Depending on the direction that the bullet hit the wall in, it'll bounce off in that direction.
                end
            else
                if a.Y < b.Y then
                    newDirection.CFrame = newDirection.CFrame*CFrame.fromEulerAnglesXYZ(0, theta, 0)
                elseif a.Y > b.Y then
                    newDirection.CFrame = newDirection.CFrame*CFrame.fromEulerAnglesXYZ(0, -theta, 0)
                end
            end
            wait()          
            script.Parent.Speed.Velocity = 20*(newDirection.CFrame.lookVector.Unit) --This is the part that messes up. When I run the script, the Speed (a bodyvelocity) is just deleted and the bullet falls through the world and is deleted. I have no idea why.
            script.Origin.Value = b --Setting a new A in the event that the bullet bounces off another wall.
            newDirection:Destroy()
        end
    end
--Please don't go off saying that I put in too many ends; I said in the premise of this question that this is an excerpt. The entire script is not shown. I know for certain that the amount of ends is proportionate to the if statements.

Answer this question