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

Shoot a ray directly below a brick?

Asked by
drahsid5 250 Moderation Voter
9 years ago

I'm working on a 2D Platformer framework with a custom player and would like to create a function that returns true if the player is close to like floor, (Like

if (player.CFrame.p - position).magnitude <= 3 then end

) but I can't get a ray to be directly below the brick I'm using as a player, it's always at a slant. Does anyone know how I would get the ray to be directly perpendicular from the brick to what's below it? My current code:

plr = script.Parent
ray = Ray.new(plr.CFrame.lookVector, (plr.CFrame.lookVector + Vector3.new(0,math.rad(-180),0)).Unit*300)
hit, position = game.Workspace:FindPartOnRay(ray)
print(hit)
print(position)
    --Test ray visual
      local distance = (position - plr.Position).magnitude
       local rayPart = Instance.new("Part", game.Workspace)
       rayPart.Name          = "RayPart"
       rayPart.BrickColor    = BrickColor.new("Bright red")
       rayPart.Transparency  = 0.5
       rayPart.Anchored      = true
       rayPart.CanCollide    = false
       rayPart.TopSurface    = Enum.SurfaceType.Smooth
       rayPart.BottomSurface = Enum.SurfaceType.Smooth
       rayPart.formFactor    = Enum.FormFactor.Custom
       rayPart.Size          = Vector3.new(0.2, 0.2, distance)
       rayPart.CFrame        = CFrame.new(position, plr.Position) * CFrame.new(0, 0, -distance/2)
0
Post your new code please TurboFusion 1821 — 9y

1 answer

Log in to vote
4
Answered by 9 years ago

A Ray has 2 parameters, Origin and Direction.

Origin

The Origin is the point where the Ray starts. In your case, it'll be the custom player, so plr.Position.

Direction

The Direction is the vector that represents what direction the ray will go in correspondence with the Origin. If the Origin is (0, 0, 0) and the Direction is (0, 5, 0), then that will create a ray that is 5 studs long that goes up starting from (0, 0, 0). In your case, you want the ray to check if the player is on the ground by checking to see if the player is a certain distance away from the ground. There's an incredibly simple way to do this, and that would be simply creating a Vector that goes a certain distance downward, like so:

local groundRay = Ray.new(plr.Position, Vector3.new(0, -5, 0)) --This will create a ray that goes down 5 studs starting from the position of the plr

Now that we have a ray, we can check to see if that ray has hit anything by using the FindPartOnRay(Ray, Ignore) method

local H, P = game.Workspace:FindPartOnRay(groundRay, plr) --We make the ray ignore the plr so that way the ray will never accidentally detect the plr instead of the ground below it

Now you have a hitpart and a position of where the ray hit. You can check to see if the hitpart exists and if it does, that means there's an object 5 studs below the plr. You can change the distance by changing the number inside the direction from 5 to whatever distance you want the threshold to be at.

Hope this helped!

1
It's still slanted, http://prntscr.com/7n994a Same result. drahsid5 250 — 9y
1
Your brick is also slightly slanted. adark 5487 — 9y
0
Looks like that was it. Weird, I never rotated it when moving it up, well, anyways thanks. drahsid5 250 — 9y
Ad

Answer this question