Your original code
obj.Position = Vector3.new(player.Character.Head.Position.X,player.Character.Head.Position.Y ,player.Character.Head.Position.Z)
Can actually just be equated to
obj.Position = player.Character.Head.Position
And it will do the same thing.
Now as for moving a part in the direction the player's head is facing, we'll need the LookVector of the head, and the easiest way to get a LookVector is to get the CFrame of the head.
You might be wondering: What's a LookVector? A LookVector is the forward direction of a CFrame. In other words, it points at something. A LookVector is a Vector3 with a Magntiude of 1.
Now that we know that, we can implement it. This is the code I wrote to move a part so and so amount of studs in the direction our character is facing.
local Const = 5
obj.Position = obj.Position + player.Character.Head.CFrame.LookVector * Const
You might be thinking: Why are we multiplying the LookVector?
The answer is because the LookVector's magnitude is only 1, if we don't multiply it, it will only go in the direction we are facing one stud.
You also might be wondering why there is a obj.Position = obj.Position in there, doesn't that seem redundant?
What I am doing is incrementing the variable.
Think of it like this:
local var = 5
var = var + 5
local var2 = 5
var2 = 5 + 5
So all I'm doing is taking our obj position, and adding the amount of studs I want to move forward by in the direction of our character's head.
If you have any questions, just comment below. I got on a quite weird explanation, I'm really tired so sorry if it doesn't make any sense lol.
1 comment
Thanks for the answer! I'm going to test it out later.