Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

is there a way to change this so it moves in the opposite direction of the shockwave?

local bp = Instance.new("BodyVelocity", prt)
    bp.MaxForce = Vector3.new(100000,100000,100000)
    bp.Velocity = workspace[script.name.PN.Value].HumanoidRootPart.CFrame.lookVector * 150

GIF

is there a way to change this so it moves in the opposite direction of the shockwave

PS: [script.name.PN.Value] is the localplayer's name

1 answer

✓ Accepted answer

Difference and Direction

The difference between two numbers can also be interpreted as direction. Remember, difference just means subtraction. For example, the difference between 3 and 10 is thought to be 10 - 3. The reason I say "thought to be" is because subtraction is not commutative. In other words (10 - 3) =/= (3 - 10) because they can yield either 7 or -7. So how do you know which order to subtract in? That depends on the direction you're looking for.

Why is This Relevant?

In one dimensional space, you only have two directions (one for each end of the axis). A number line is a perfect example of one dimensional space. A negative number points to the left of the number line, and a positive number points to the right (zero has no direction). This same logic applies for higher dimensions as well, due to the wonderful properties of vector arithmetic.

This means you can also subtract vectors from each other to get a direction on a 3D scale (or higher), just like you can subtract two integers from each other to get a direction on a 1D scale. Be careful though, as I said before, subtraction is not commutative and the order in which you subtract vectors from each other matters. Vectors are just systems of numbers, so they share the same properties.

Magnitude

You may have heard this term before, but here's a quick refresher. Magnitude refers to the intensity, or length of a vector. Magnitude is actually a property of direction. If you want to know the direction of the nearest grocery store from wherever you are, that direction is a vector with a length (magnitude) of one (also called a unit vector). The distance between you and the grocery store is the magnitude. Here's a little visual representation I put together to make it easier to picture.

The Final Product

With the heavy explaining out of the way, I'll get to the implementation for what you want the final product to be. First, we want the position of the part getting pushed back and the position of the part from whence the part is getting pushed back from (the target's position, and your character's position, in this case).

Arguments:

  • origin: your position
  • target: position of the part getting pushed in the opposite direction of you
  • magnitude: how fast or how forceful the target gets pushed back
local function GetBlastVelocity(origin, target, magnitude)
	local direction = target - origin -- direction w/ magnitude
	local unitVec = direction/direction.Magnitude -- direction w/o magnitude
	return unitVec*magnitude -- direction with new magnitude
end

The unitVec variable represents the original direction of the part from your character, divided by the magnitude of that direction, which gives us the same direction but with a length of one (a unit vector). Then we return the unit vector multiplied by magnitude to determine the intensity of the blast for yourself. Knowing what GetBlastVelocity returns, simply set the velocity of your BodyVelocity equal to that.

local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = GetBlastVelocity(you.Position, target.Position, 50)

And that's all! I know I made this answer pretty long, I apologize if it was time consuming to review. I just wanted to make sure I didn't leave anything out. Feel free to leave a comment if you have any questions.

8 comments

0

I cannot thank you enough! This is amazing! I only have one problem. It is delayed at the beginning? is it cos it has to do the math so it's a little slow? I don't know how to reduce that!

1

Computers are incredibly fast at doing arithmetic, you wouldn't sense a delay from that unless your program was deliberately designed to. The delay is most likely coming from a yield function, network latency, or some other factor.

1

If you want me to take a look at it, maybe we could do a team create together in an empty place?

0

I see, Thanks a ton!

0

and sure, do you have a discord? Mine is Red#3230

1

Yup, I'll be sure to add you.

0

Thank you!

1

Mine is Willrus#4068

This discussion is preserved as it was. New answers, comments, and votes are closed.