Getting the position that is always in front of a character?

Hey developers!

I am trying to spawn a part 6 studs in front of the character, no matter how it’s rotated.

Here is my current code:

local Model = workspace.Rig

local Part = Instance.new("Part")
Part.Size = Vector3.new(0,0,0)
Part.Parent = workspace


Part.Position = Model.HumanoidRootPart.Position + Vector3.new(0,0,-6)

At the moment the part isn’t always spawning in front of the player. I’m not that experienced with vector and cframe. but does anyone have a fix for this?

All contributions appreciated! :smiley: Thanks!

3 Likes

You can use CFrame.LookVector and do some vector calculations to get The position 6 studs in front.

and CFrame.LookVector will return a Vector value means no rotation.

1 Like

maybe use the CFrame instead of Position?

local function FindPosition(RootPart, OffsetStuds)
    -- Add the multiplied direction to the current position
    return RootPart.Position + RootPart.CFrame.LookVector * OffsetStuds
end
4 Likes

I would do it like this:


local p = Instance.new("Part",workspace)
p.Position = (game.Players[SomePlayer].Character.HumanoidRootPart*CFrame.new(0,0,-6)).p
-- or
-- p.CFrame= game.Players[SomePlayer].Character.HumanoidRootPart*CFrame.new(0,0,-6)

8 Likes

Forgot for a hot minute that Instance has 2 arguments. :joy:

5 Likes

Sorry for the bump but I feel like it is necessary for anyone in the future looking at this to know that the code there is incorrect.

The code should be:

p.Position = (game.Players[SomePlayer].Character.HumanoidRootPart.CFrame*CFrame.new(0,0,-6)).p