Raycast casting to the wrong direction

In my game, there is a move called “Shockwave” which hits characters near the character who used it if the character’s fell on the ground at a high velocity.

Due to client-server delay, the position of the effect of the shockwave appears wrongly, and I need to use a raycast to fix the position of both: shockwave effect and shockwave hitbox.

However, in my game for some reason every time I ever used raycasts they go to the wrong direction for no reason. I had to give up of making many abilities just because of this issue.

Also, some effects that use the HumanoidRootPart’s LookVector to position itself appear looking at the wrong direction too. Talking about this because I use the HumanoidRootPart to make a direction for the raycast, so both of those problems could be related to each other.

Here is the part of the code which raycasts and positions the effect to the raycast’s .Position and a screenshot of where the effect is appearing.

                        local params = RaycastParams.new()

                        params.FilterType = Enum.RaycastFilterType.Exclude
                        params.FilterDescendantsInstances = {workspace.Characters}

                        local result = workspace:Raycast(humanoidRootPart.Position, humanoidRootPart.Position - Vector3.new(0, 25, 0), params)

                        if result then
                            local shockwaveModel = movesStuffFolder.Models.Shockwave:Clone()
                            
                            shockwaveModel.Size = Vector3.new(5, 1, 5)
                            shockwaveModel.CFrame = CFrame.new(result.Position + Vector3.new(0, shockwaveModel.Size / 2, 0), humanoidRootPart.CFrame.LookVector)
                            shockwaveModel.Parent = workspace
                            [...]

image

The second parameter of Raycast is the direction you want the ray to travel.

The one you have there humanoidRootPart.Position - Vector3.new(0, 25, 0) is a pretty nonsensical direction.

Normally people use (originPosition, targetPosition - originPosition) to point towards a specific position, or (originPosition, somePart.LookVector * length) to point in a particular direction.

How would I get the down direction of the HumanoidRootPart relative to the world? I subtract that Vector3 from the HumanoidRootPart’s position because it points downwards world relative with lenght of 25 studs.

Also, I just changed it to this:

local result = workspace:Raycast(humanoidRootPart.Position, humanoidRootPart.CFrame.UpVector * -25, params)

It works now. The problem is that, if the character gets hit mid air and the HumanoidRootPart’s UpVector changes, the raycast will be pointing to the wrong direction. This is why I need to point the ray downwards world relative.

You could just use Vector3.yAxis * -length so that the ray always goes downwards no matter the orientation of the HRP.

What you have there is “always point out the bottom of the part”

You could do Vector3.new(0, -25, 0) to “always point in the direction of gravity”

Like this?

local result = workspace:Raycast(humanoidRootPart.Position, Vector3.new(0, -25, 0), params)

Wouldn’t this make it look to the position Vector3.new(0, -25, 0) instead of the position of the HumanoidRootPart minus 25 studs in the Y axis? I need it to be specifically 25 studs below the HumanoidRootPart but also in the same X and Z axis of the HRP.

Nope. You’re supposed to give it a direction not a position.

Then yes, what you have there is correct:

local result = workspace:Raycast(humanoidRootPart.Position, Vector3.new(0, -25, 0), params)

Will raycast downwards 25 studs on the world’s Y axis.

1 Like

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