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

Ray acting weird? [no other way to explain]

Asked by
NotSoNorm 777 Moderation Voter
8 years ago

My best explanation is that it's taking the position and just like, Flipping it over the character as if it was an axis.

https://gyazo.com/50cebe1227a462c073e1063b61f5bcb3

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local torso = character.Torso
local walls = game.Workspace.Walls:GetChildren()

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.E then
        local lowest = 20
        local low
        for _, wall in pairs(walls) do
            local distance = (torso.Position - wall.Position).magnitude
            if distance < lowest then 
                lowest = distance
                low = wall
            end
        end
        print(low)
        if low then
            -- low may not be set if the closest is further than 20 studs
            local ray = Ray.new(torso.Position, (low.Position - torso.Position).unit)
            local hit, position = game.Workspace:FindPartOnRay(ray)
            local distance = (low.Position - torso.Position).magnitude
            local rayPart = Instance.new("Part", player.Character) -------
             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,(torso.Position - low.Position)) * CFrame.new(0, 0, distance/2)
            for i = 1, #walls do
                walls[i].BrickColor = BrickColor.new("Smoky grey")
            end
            low.BrickColor = BrickColor.Green()
        end
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

The second parameter to CFrame.new(from, to) is not the direction. It's the point to look towards.

Thus setting the rayPart.CFrame should look something like this:

 rayPart.CFrame = CFrame.new(position, torso.Position) * CFrame.new(0, 0, distance/2)

Your distance should also be computed using position instead of low.Position.


Your FindPartOnRay is also not quite what you want.

FindPartOnRay only looks the length of your ray, so you need it to have a long length:

 local ray = Ray.new(torso.Position, (low.Position - torso.Position).unit * 999)

This will look at least 999 studs, instead of just 1 (where it isn't going to hit anything except maybe your arm).

You'll also probably want to ignore your character, so that the ray doesn't hit any part of you on its way to the wall:

local hit, position = game.Workspace:FindPartOnRay(ray, character)

You can debug these problems by adding a print(hit).

nil means the ray isn't reach anything. Something like "Right arm" or some other part suggests you're hitting something along the way.

Ad

Answer this question