Hello, I want to make it so that it raycast any objects in front of the player so that if I teleport the character in front, so character wouldn’t get stuck like a wall for example.
Here’s what I’ve achieved so far:
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {character, fx}
local raycastResult = workspace:Raycast(humanoidRootPart.CFrame.Position, humanoidRootPart.CFrame.LookVector * 40, params)
if raycastResult and not raycastResult.Instance then
humanoidRootPart.Position = humanoidRootPart.Position + Vector3.new(humanoidRootPart.CFrame.LookVector.X * 40, 0, humanoidRootPart.CFrame.LookVector.Z * 40)
end
I don’t think I’m doing it right because there can be no walls in front of me and it still doesn’t run it. I’m new to raycasting so any feedback is appreciated.
Have you checked printing it? I think the issue isn’t with Raycast (you doing everything right), the problem is that you’re changing HumanoidRootPart position, not CFrame. If you only change its position, it doesn’t move the rig with it. You need to change its CFrame.
Good advice, however, the ray cast is the problem because it supposed to ray cast if there’s any object ahead before moving the character. I simply used position to see if my raycast works.
I tried this and it only works if there is object in front. Which is kinda weird or I just don’t know what I’m doing lol.
if raycastResult then
print("there is result", raycastResult.Instance)
if raycastResult.Instance ~= nil then
humanoidRootPart.Position = humanoidRootPart.Position + Vector3.new(humanoidRootPart.CFrame.LookVector.X * 40, 0, humanoidRootPart.CFrame.LookVector.Z * 40)
end
end
Oh, but if it doesn’t find anything, the “raycastResult” will return nil, that is the issue I guess. You’re searching for the case when the variable returns nil.
Isn’t a raycast always supposed to have an instance? According to the post I found, raycastResult would be nil if there is no instance. (or did you already try that)
I tried seeing if raycastResult.Instance == nil does anything but it doesn’t do anything. Which is strange. I also printed the raycastResult.Instance but if there is no instance it also doesn’t print anything.
since you want to check if there is nothing infront, you should check if “not raycastResult” is true, as that would return true if raycastResult is nil and thus signify there is no hit
I got a question; how I can make the player position get close to the raycastResult.Instance let say 5 studs away from it but they don’t get stuck in the wall?
You’d want to find the intersection point of the raycastResult.Instance and the line going from the center of the raycastResult.Instance and the player, and move it 5 more studs towards the player.
I’ll check to see if theres an easy way to do that
Aha found it
Theres a raycastResult.Position which gives you that intersection point. Now just move it 5 studs towards the player using the location of the player and some trigonometry