Spawn object in the direction the player is looking

Hi
I want to spawn a part 5 studs infront of where the player is looking and then add velocity to that part to go to the direction the player is looking. I got the spawning snd the add velocity part I just dont know how ti get direction of the player looking.

1 Like

use LookVector to get the direction

I cant get it to work with vector3 only cFrame

whats the problem with vector3? you can use it for positions etc too

You can use the CFrame of the HumanoidRootPart to get the look vector. Although you can use the head to get it, the head sometimes animates so it might go in the wrong direction:

local LookVector = Character.HumanoidRootPart.LookVector
Part.Position = Character.Head.Position + LookVector * 5 -- Places a part 5 studs in front of the head
3 Likes

Thanks ill try it out after school

1 Like

You can set the paths velocity to the players LookVector and then multiply it to increase the force of the throw.

local dir = Character.PrimaryPart.CFrame.LookVector
local force_multiplier = 10
local force = dir * force_multiplier

part:ApplyImpulse(force)
3 Likes
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")

task.wait(3)

local part = Instance.new("Part")
part.Parent = workspace
part.Anchored = true
part.CFrame = hrp.CFrame * CFrame.new(0, 0, -5)

This is a local script.

2 Likes

Thanks for replying, but from what I see and tested everyone is using HumanoidRootPart.CFrame.LookVector which spawns the object infront of player’s torso. Is there any way to get the direction where the player is looking at with their camera not the direction they are facing? Or if not then maybe the direction of the cursor?

1 Like

You can use the camera’s CFrame.

workspace.CurrentCamera.CFrame.LookVector

to spawn part in the camera direction

local CameraLookVector = workspace.CurrentCamera.CFrame.LookVector * stud
Part.CFrame = HumanoidRootPart.CFrame * CFrame.new(CameraLookVector.X,0,CameraLookVector.Z)
3 Likes

Thanks for replying, I tried it out and it seems to work but whenever the player moves the position changes if that makes sense. Whenever I press W and look in one direction then it spawns in front but when I press A and look in the same direction as before it spawns on the right.

1 Like

use this instead of HumanoidRootPart.CFrame

CFrame.new(HumanoidRootPart.Position)

this is to set the angles to 0

Works great! Thank you so much, btw do you know how would I use it to add velocity to the cloned part?
My code right now looks like this:

local shareMassPoint = game.ReplicatedStorage.SharedMass:Clone()
			shareMassPoint.Parent = game.Workspace
			shareMassPoint.CFrame = CFrame.new(ball.Position) * CFrame.new(CameraLookVector.X,0,CameraLookVector.Z)

			shareMassPoint.BodyVelocity.Velocity = Vector3.new(25, 0, 0)

			repeat
				shareMassPoint.BodyVelocity.Velocity -=  Vector3.new(2, 0, 0)
				wait(0.1)
			until shareMassPoint.BodyVelocity.Velocity.X <= 0

			shareMassPoint.BodyVelocity.Velocity = Vector3.new(0, 0, 0)

Part at the bottom is for adding velocity and then making it stop after time, It is using Vector3 and it’s set, I can’t figure out how to use the camera look vector with it.

you can set the velocity to CameraLookVector. also you could use tween to slow the body velocity down instead of loop

1 Like

Thanks I will use a tween service, I can’t figure out how to get the goal CFrame though, I tried multiplying the CFrame, tried adding but nothing seems to work. (I am very bad at working with CFrames)

I got this basic code from TweenService page in the api reference and changed the goal.

local goal = {}
			goal.CFrame = CFrame.new(ball.Position) * CFrame.new(CameraLookVector.X,0,CameraLookVector.Z)

			local tweenInfo = TweenInfo.new(5)

			local tween = TweenService:Create(shareMassPoint, tweenInfo, goal)

			tween:Play()

(I will change all the easing styles, repeating and stuff later)
And the “ball” is a part which is inside the player character which is attached to the humanoidRootPart.
In short the player is a ball.

CameraLookVector *= distance -- (in stud)

for offset.

1 Like

It shows *= is incorrect and when putting just the * sign it give an error saying expected Vector3 got number

goal.CFrame = CFrame.new(ball.Position) * CFrame.new(CameraLookVector.X,0,CameraLookVector.Z) * 10

multiply the look vector, you can’t multiply cframe. in the goal you are using cframe, you can do

CFrame.new(Vector3.new(CameraLookVector.X,0,CameraLookVector.Z)*10)

or

CameraLookVector *= distance

in the outside.

1 Like

Thank you so much! I got it working by doing this:

goal.CFrame = CFrame.new(ball.Position + Vector3.new( CameraLookVector.X,0,CameraLookVector.Z)*10)

Thanks again man, I am sorry for wasting so much of your time!