Help with velocitys based on my camera direction

Hello, I’m currently making a snowball system for my game for a holiday event. I’ve never worked with body velocity on things beside characters. How would I make an object go in the direction of the camera?
I tried finding solutions for this but nothing is what I need.

Ex: Players looking behind them with their camera, they throw the snowball, the snowball goes in that direction.

1 Like

The direction is just the lookVector of the camera’s CFrame. That’s what you would set the body velocity to multiplied by a speed.

This is my current code.

local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local uis = game:GetService("UserInputService")
local hum = Char:WaitForChild("Humanoid")
local anim = hum:LoadAnimation(script.GrabSnowballAnim)
local anim2 = hum:LoadAnimation(script.Throw)
local CanGrabSnowBall = true
local Camera = game.Workspace.CurrentCamera
local Speed = game.Workspace.Folder.Part.Value.Value


local function SnowBall(plr, SnowBall)
	if CanGrabSnowBall == true then
		CanGrabSnowBall = false
		anim:Play()
		wait(1)
		anim:Stop()
		game.ReplicatedStorage.Events.TakeSnowBall:FireServer(plr, SnowBall)
		uis.InputBegan:Connect(function(input, gameProcessedEvent)
			if input.KeyCode == Enum.KeyCode.T then
				game.ReplicatedStorage.Events.ThrowSnowBall:FireServer(plr, SnowBall)
				anim2:Play()
				local ThrowForce = Instance.new("BodyVelocity", SnowBall)
				ThrowForce.Name = "Velocity"
				ThrowForce.Velocity = Camera.CFrame.LookVector*Speed
				
				
				wait(1)
				CanGrabSnowBall = true
				anim2:Stop()
			end
		end)
	end
end

game.ReplicatedStorage.Events.ClientSnow.OnClientEvent:Connect(SnowBall)```

If you are trying to make events synced with animations you can use YourAnimationTrack:GetMarkerReachedSignal("YourAnimationEvent"):Connect() instead of the wait().

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
Mouse.Button1Down:Connect(function()
	local part = Instance.new("Part")
	part.Anchored = false
	part.CanCollide = false
	part.CFrame = Camera.CFrame
	part.Parent = workspace
	part.Velocity = Camera.CFrame.LookVector * 500 -- LookVector * Speed
end)

Here is example that you can put in LocalScript to try it out. It will throw a Part in the direction of the the Camera. However for what you doing I would recommend using Mouse instead of Camera.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
Mouse.Button1Down:Connect(function()
	local part = Instance.new("Part")
	part.Anchored = false
	part.CanCollide = false
	part.Shape = Enum.PartType.Ball
	part.Size = Vector3.new(1, 1, 1)
	part.Material = Enum.Material.Sand
	part.BrickColor = BrickColor.White()
	part.CFrame = Character.RightHand.CFrame
	part.Parent = workspace
	part.Velocity = CFrame.new(part.Position, Mouse.Hit.Position).LookVector * (workspace.Gravity * part:GetMass())
end)

This LocalScript will create a snowball and it will throw it from player’s right hand to the direction of where Mouse is pointing to; you can change the Character.RightHand to something else if you want to, you can also use UserInputService instead of Mouse.


You can adapt either of these methods to your script. I hope this helped you, otherwise; happy coding :wink:.

3 Likes

Thanks! :smiley: I really needed this.

1 Like