How To Raycast Directly in Front of HumanoidRootPart?

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.

That didn’t work. Any other methods? :sweat:

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.

--Change the line:
humanoidRootPart.Position = humanoidRootPart.Position + Vector3.new(humanoidRootPart.CFrame.LookVector.X * 40, 0, humanoidRootPart.CFrame.LookVector.Z * 40)
--for:
humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position + Vector3.new(humanoidRootPart.CFrame.LookVector.X * 40, 0, humanoidRootPart.CFrame.LookVector.Z * 40))

I guess it will works.

Edit: Analyzing your code, I saw you could write just:

humanoidRootPart.CFrame = humanoidRootPart.CFrame * CFrame.new(40,0,40)

I agree with Kakazin. You could also try printing raycastResult.Instance to see if theres anything in there.

(also, im assuming by “that didn’t work” you meant that replacing the if line with “if not raycastResult” didn’t work)

1 Like

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

In the script you send, you put:

if raycastResult and not raycastResult.Instance then

Isn’t that the issue? I mean, are you searching for a situation that Raycast exists but doesn’t return a Instance?

Yes, it doesn’t return an instance.

1 Like

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)

1 Like

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.

yea, because raycastResult.instance is never nil, if raycastResult.instance doesn’t exist, raycastResult (not the instance) is nil.

So should I just check if raycastResult is false or nil or something?

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

1 Like

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

Alright thanks, I don’t really know how to find the intersection point. :sweat:

this thing right here :smiley: (sadhakjsdhaskjhdasjhd)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.