Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Make an object's velocity relative to player?

Hello, I've been trying to script a ball that flies after the player clicks it. I've tried a script where the ball actually does leave the hand, but it doesn't go

This was the script I had:

local ball = script.Parent.Handle
local tool = script.Parent
local character = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").LocalPlayer.CharacterAdded:wait()

tool.Equipped:connect(function(Mouse)
	Mouse.Button1Down:connect(function()
			local b = ball:Clone()
			b.Parent = game.Workspace
			local P = Instance.new("BodyPosition", b)
			P.Position = character.Head.Position + Vector3.new(0, 0, 0.5)
			local V = Instance.new("BodyVelocity", b)
			V.Velocity = Vector3.new(1, 0, 0)
			wait(.1)
			tool.Parent = game.ServerStorage
	end)
end)

In this script, the ball did fly through the air. However, I want to change it so that instead of the ball having to follow the axes, and it always go in the same direction, it will always move directly in front of the player. (I don't have very much experience with this type of scripting)

How could I change

V.Velocity = Vector3.new(1, 0, 0)

so that the ball always in what the player sees as in front of them?

1 answer

Typically for velocity you want to supply a direction. All vectors have a direction, but it is made more clear with the unit vector, because everything is in ratio 0-1. CFrames are composed of these unit vectors. The CFrame lookVector is the axis a BasePart is looking. Supply the head's direction like so:

V.Velocity = character.Head.CFrame.lookVector

You can multiply this by any number to resize the vector. For example:

V.Velocity = character.Head.CFrame.lookVector * 5

BTW it seems weird to have BodyPosition and BodyVelocity at the same time. Bodyp is meant to keep a part in one place. This is all up to you though!

2 comments

0

This script makes the ball go sideways, and it does't nearly go fast enough. How would I fix this?

0

Told you how to increase speed. And what do you mean by sideways? Part moves the wrong direction? This is perfect velocity code. again try removing the BodyPosition, OR change MaxForce properties. Let me know what you do

This discussion is preserved as it was. New answers, comments, and votes are closed.