So im trying to make a flying cloud, and when a player clicks W, I want the cloud to go forward. Im using body velocity for this. So how would I find the place in front of someone and set the body velocity to that?
In order to move the object in the direction the player is facing, you can use the lookVector
property of CFrames.
One Local Script example would be:
local player = game.Players.LocalPlayer game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.W then workspace.Cloud.BodyVelocity.Velocity = player.Character.HumanoidRootPart.CFrame.lookVector end end)
If you want this event to occur on the server, you would need to use Remote Events
Note that the above function takes into account the direction the player was facing when key "w" was pressed and doesn't follow the player's change in direction.
In order to make sure the block was facing the player at all times until "w" was not pressed, you could use:
local player = game.Players.LocalPlayer local prevent = false game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.W then repeat workspace.Cloud.BodyVelocity.Velocity = (player.Character.HumanoidRootPart.CFrame.lookVector) * 1 -- Determines speed at which the block moves wait() until prevent == true end end) game:GetService("UserInputService").InputEnded:Connect(function(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.W then prevent = true workspace.Cloud.BodyVelocity.Velocity = Vector3.new(0, 0, 0) -- Remove this line if you want the cloud to continue moving wait() prevent = false end end)
Closed as Not Constructive by TheeDeathCaster
This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.
Why was this question closed?