Currently, my ball has a VectorForce, and using this post from Egomoose, I’m calculating the initial velocity required to launch the ball to its target.
However, the target in my case is not where the player clicks, but is instead a certain distance away from the player, with the longer the player clicks and holds, the further the target distance is.
However, I’ve encountered several problems trying to get the direction the player is facing, so that the ball is launched in the same direction the player is facing.
--[[
t= target position
d=duration
Angle=player camera angle
g=gravity
]]
-- When t is a vector it returns as 0,0,0
local t = CFrame.new(Character.HumanoidRootPart.Position + Character.HumanoidRootPart.CFrame.LookVector * Power*100)
local o = Vector3.new(Character.HumanoidRootPart.CFrame)
local g = Vector3.new(0, -game.Workspace.Gravity, 0)
local d = 1
-- 0.5 meaning the angle of the throw is fixed at 30 degrees, change this later
local velocity = (t-o-0.5*g*d*d)/d
--(t+0.5*g*d*d-o)/d
return velocity
The current problem with my attempt
TLDR; How am I supposed to throw the ball in the direction where the player is facing
I’m not sure if the is the problem, but I don’t think t’s orientation is correctly factored
local t = Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-power * 100)
--also o can be simplified to this
local o = Character.HumanoidRootPart.Position
I had just solved it by adding .p
to the end of local t=...
.
However, the ball isnt actually being thrown in an arc.
But after that, I have no idea how I could fix it?
https://gyazo.com/e442eaa4dd9127409597274faf5f2215
Edit: The solution below works perfectly as well, however, it has the same problem with the ball not traveling in an arc
after reading a bit more into the EgoMoose Post i think t should be
local t = Character.HumanoidRootPart.CFrame * Vector3.new(0,0,-power * 100)
This is so the result is a vector, which is divisible by a number. He also includes an offset on o so that the ball comes from the hand and not the hrp.
How are you setting the velocity of the part?
local function InitialVelocity(Power, Character)
--[[
t= target position
d=duration
Angle=player camera angle
g=gravity
]]
-- When t is a vector it returns as 0,0,0
local t =Character.HumanoidRootPart.CFrame * Vector3.new(0, 0,-Power * 100)
local p= Instance.new("Part", workspace)
p.Position = t
p.Name = "marker"
print(t)
local o = Character.HumanoidRootPart.Position
local g = Vector3.new(0, -game.Workspace.Gravity, 0)
local d = 1
-- 0.5 meaning the angle of the throw is fixed at 30 degrees, change this later
local velocity = (t-o-0.5*g*d*d)/d
--(t+0.5*g*d*d-o)/d
return velocity
end
function ThrowPaint_Module.new(Power, Character) -- Create paint object
-- Vector force
Paint.VectorForce = Instance.new("VectorForce", Paint.Obj)
Paint.VectorForce.Force = InitialVelocity(Power, Character) -- Call the function to calculate initial velocity
Paint.VectorForce.Attachment0 = Paint.Attachment
Paint.VectorForce.RelativeTo = Enum.ActuatorRelativeTo.World
return Paint
end
I also decided to check where the target position t
, was being placed, it’s not quite right to say the least.
https://gyazo.com/7f349cfaf343bbf5b5597de606ce4b52
based off the y height of 3, i dont think the y component for the velocity is high enough, which is why it just drops to the floor. You could do something like add
t+= Vector3.new(0,power * math.sin(math.rad(30)),0)
I’m not sure if that will need a scalar.
Also, I think the ball has just 1 impulse not a constant vector force, use AssemblyLinearVelocity to set it. The constant vector force is what is increasing the velocity of the ball because F=MA, so just set the velocity of the part once.
1 Like
This solution seems to work well, however, the post on AssembleLinearVelocity is pretty bare and I can’t seem to find any example of how it’s used online, could you provide one?
I do agree it is very poorly documented, but its basically just the replacement to the Velocity property of the part.
part.AssemblyLinearVelocity = Velocity