Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I cast a ray and raise the position of the origin point?

I'm trying to cast a ray at head level. I would do ...

local rayDirection = script.Parent.Head.CFrame.LookVector*10

... but I have it so that the head moves relative to the mouse hit position. I have it so that it casts out from the HumanoidRootPart since the HRP always rotates towards the character move direction.

local ray2Direction = HRP.CFrame.LookVector*10

How can I go about raising this CFrame up by like a stud or two so that it's head level?

JesseSong y u bully

1 comment

0
  • CFrame.new(0,3,0)

1 answer

✓ Accepted answer

To cast a ray, you use the workspace:Raycast() function.

The parameters for this is the origin, direction, and a RaycastParams.

To make a RaycastParams, just do local params = RaycastParams.new() and set the values of it.

The values are: FilterDescendantsInstances, which is a table of Instances you want to filter from the raycasting, FilterType is an Enum.RaycastFilterType and is either Whitelist or Blacklist. Whitelist means that the ray can only detect things in the FilterDescendantsInstances, and Blacklist means that the ray can detect everything except the FilterDescendantsInstances.

It also has IgnoreWater, which is a bool value you set to true if you want the ray to ignore water terrain. Lastly, CollisionGroup. "Parts in collision groups that are set to not collide with this group will be ignored". You do not have to set this value. If you don't it defaults to the default collision group.

The Raycast() function returns a RaycastResult, which holds the position of where the ray hit the part (result.Position), the instance it hit (result.Instance), the material of the instance (result.Material), and the normal direction of the face it hit (result.Normal). That is basically just the direction it's facing.

To raise the position of the origin point, just add a Vector3 to your original. The Vector3 will be: Vector3.new(0, amount, 0, amount being however many studs you want the origin to be raised.

So, you would do origin = HRP.Position + Vector3.new(0, amount, 0)

Since the head is 1.5 studs over the center of the HRP, you would add Vector3.new(0, 1.5, 0) And, you should also make sure you ignore the head of the player.

This discussion is preserved as it was. New answers, comments, and votes are closed.