Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I find the place in front of someone? [closed]

Asked by 5 years ago

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?

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?

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

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)
0
you go reputation manipulator ! User#24403 69 — 5y
0
Back at it again are you? I simply answered a question with an example, of all people you should know the lookVector of the CFrame is what is required SerpentineKing 3885 — 5y
0
Im trying to use this script on a local script and its not working? CaptainD_veloper 290 — 5y
0
where is your cloud located? This example is determinant on the fact that the cloud is in the workspace. If you are using the second example, make sure you have a boolvalue under the script named "Prevent" as well. In both cases the cloud needs to be unanchored for the bodyvelocity to work SerpentineKing 3885 — 5y
0
it's cuz he's cheating reps User#24403 69 — 5y
Ad