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

I am having trouble with raycasting/the camera?

Asked by 4 years ago

Having to always put a question mark in the title is a bit annoying. Anyways...

I haven't tried raycasting in a while, and now that I've gotten back to it I seem to have forgotten how raycasting works. In the code below look is the LookVector of the LocalPlayer's camera. For context, the camera is locked in first person. Whenever I try to raycast, it always intersects the baseplate no matter what ( even when I look at the sky ). After doing a bit of testing I found out that the camera's LookVector is always the same, and I don't understand why. Shouldn't it change when I look in two different points?

local event = game.ReplicatedStorage.Events.Shoot

event.OnServerEvent:Connect(function(player,look)
    print("server recieved")
    print(look)
    local pos = player.Character.Head.CFrame.Position
    local ray = Ray.new(pos,look*100)
    local ignore = player.Character
    local hit = workspace:FindPartOnRay(ray,ignore)
    if not hit then return end
    print(hit.Name)
    print(hit.Parent.Name)
    local hum = 0
    if not hit.Parent ~= game.Workspace and hit.Parent:FindFirstChild("Humanoid") then
        hum = hit.Parent.Humanoid
    end
    if hum ~= 0 then
        print("humanoid detected")
        if hit.Name == "Head" then
            hum:TakeDamage(100)
            print("damage dealt")
        else
            hum:TakeDamage(40)
            print("damage dealt")
        end
    end
end)

How else should I raycast in the direction the camera is facing at if LookVector itself doesn't seem to work?

Answer this question