AssemblyLinearVelocity help needed

Hey, I’m creating a throwable grenade for my game and have run into an issue. When the player presses “E”, the grenade should be thrown in front of them. The issue is that if the player rotates their character, the grenade launches behind them or to the side. I’m not quite sure how to fix this, but believe that I may need to use something called LookVector? Or maybe I should be using LinearVelocity, VectorVelocity, etc. instead of AssemblyLinearVelocity? Again, I’m not sure, so any help is much appreciated.

LocalScript: (I will serverside the physics once I figure this out)

local UIS = game:GetService("UserInputService")
local LocalPlayer = game:GetService("Players").LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local tool = script.Parent
local character = LocalPlayer.Character

UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping == false then
		if Input.KeyCode == Enum.KeyCode.E and tool.Parent.Name ~= "Backpack" then
			local grenadeClone = ReplicatedStorage:WaitForChild("GrenadeModel"):Clone()
			grenadeClone.Parent = workspace
			
			-- // The two most important lines.
			grenadeClone.PrimaryPart.CFrame = character.ProjectileSpawn.CFrame -- // ProjectileSpawn is a part which is parented to the player's character when they join.
			
			grenadeClone.PrimaryPart.AssemblyLinearVelocity = Vector3.new(0,0,-30) -- // Applies velocity to the grenade.
		end
	end
end)

Just do HRP.CFrame.LookVector*30, vector3 doesnt account for orientation.

1 Like

No that would literally throw the grenade in front of the player character and not where the player is aiming which is how it works in 99% of games

1 Like

So the direction would be:
(Mouse.Hit.Position - Camera.Focus.Position).Unit * 30

To get the Mouse do local Mouse = Player:GetMouse()
To get the Camera do Camera = workspace.CurrentCamera

And I would recommend increasing the velocity to around 75 - 125 because 30 isn’t fast enough

1 Like

And also, have a Remote Event for the grenade on the server and send the direction because that grenade can only be seen on the client and explode on the client which would do 0 damage to other players

1 Like

Works perfectly, thank you! I’ve always had so much trouble with the mouse and camera apis. I don’t know why it’s so difficult for me to learn those

1 Like

In my OP, your code was the solution as I wasn’t sure how to use the mouse. However, I realize Larry’s method is much a much cleaner method of doing it. Thank you for your reply though

1 Like

Thanks! Remember to mark as solution! :slightly_smiling_face:

Sorry! That’s the first time I ever forgot to do that :sweat_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.