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

How Would I Go About Pushing Players Away From a Contact Point on a Sphere?

Asked by 5 years ago

I am essentially trying to make a script that pushes any player that comes into contact with a sphere away from it in the opposite direction they came into contact with it. Even if the player comes at it facing backwards, the sphere should still push them away from it. Is there any way to do this? Any help would be appreciated, as I'm not even sure where I'd start with this.

1 answer

Log in to vote
0
Answered by
pidgey 548 Moderation Voter
5 years ago
Edited 5 years ago

Firstly, you need to get the distance from the character to the object while somehow having an orientation the same as the part looking at the character. We need this because we need to tell which direction the player will move to relative to the object. This can be done with PointToObjectSpace.

--part would be the object (sphere), root would be the HumanoidRootPart from the character who's touching the part.
direction = part.CFrame:PointToObjectSpace(root.Position).Unit

direction is the distance and direction the part is relative to the character (root). I use the Unit function of Vector3 because I want to multiply this value by how many studs we want to travel.

distance = 5 --studs
direction = part.CFrame:PointToObjectSpace(root.Position).Unit
character:MoveTo(root.Position + direction * distance)

Here you see a multiplication in the direction unit to move it the distance. We add the current character's position (root) to keep the character in the same position it was in before, just offset by the direction and distance. CFrame Math Operations --> PointToObjectSpace if you want to learn more of how that works. Hope this helped!

0
So great! Thank you so much! corncob567 275 — 5y
0
Is there a way to use something other than MoveTo so that the movement is more fluid? corncob567 275 — 5y
0
BodyVelocity. Put one inside the root of the character, then set the velocity equal to (direction * distance) / t. direction * distance is studs/s - Divide that by the amount of time you want the character to get to that point by. pidgey 548 — 5y
0
so speed = 1; Velocity = (direction * distance) / t; wait(t); BodyVelocity:Destroy(). 't' would be the time it takes to get there. Hopefully that makes sense in-line. If you want more explanation, head into the Community Chat pidgey 548 — 5y
View all comments (7 more)
0
Use BodyForce if you want to include gravity into the equation. pidgey 548 — 5y
0
I tried this and it seems to be working, but the force is very weak and slow. As in, I can easily walk against it. How should I adjust it to fix this problem? corncob567 275 — 5y
0
So if you're using BodyForce that would happen because of gravity and the player moving. Set the player's WalkSpeed and JumpPower to 0, then set it back to what it was after 't' seconds. For the friction that occurs against parts with your character, you could just try and add extra force. pidgey 548 — 5y
0
So it could look like: extraForce = 150; Velocity = (direction * distance) / t + extraForce. Another solution could be to use BodyVelocity, except that the Y in Vector3 would be set to 0 so gravity is factored in again (yes it works like that, don't know why). pidgey 548 — 5y
0
Continuing on from BodyVelocity, it could look like this: v3 = (direction * distance) / t; Velocity = Vector3.new(v3.X, 0, v3.Z) pidgey 548 — 5y
0
I'm using BodyVelocity, and I just can't get it to go fast. It always seems to go a slow speed of like 2 studs per second no matter what I change. corncob567 275 — 5y
0
For anyone else reading, it got solved. You will most definitely need to have the appropriate MaxVelocity and Power for BodyVelocity to move faster/slower. pidgey 548 — 5y
Ad

Answer this question