So Basically am making a soccer game, and in my script add velocity to the ball when the ball touches the hit box and when the m1 button is pressed. The problem here is that I calculate the direction of player using move direction and move dir will only return when it is moving. I have tried other ways like getting the look vector of the character hrp or camera but it was of no use. When the character moves, stops and rotates quickly and hit the m1 button, the ball goes in the wrong direction. So am finding a way to calc direction other than using lookvector.
Hers the footage of what exactly the problem is
Hers my script for ball:
local RS = game:GetService("RunService")
local ball = script.Parent
local m1Event = game.ReplicatedStorage.RemoteEvents:FindFirstChild("MouseButton1Event")
local defaultForce = ball:GetAttribute("Force")
local force = defaultForce
local hitObject = nil
local db = true
function OnTouchFootball(hit)
if hit.Name ~= "HitBox" then return end
hitObject = hit
end
function OnTouchEndedFootball(hit)
if hit.Name == "HitBox" then
hitObject = nil
end
end
ball.Touched:Connect(OnTouchFootball)
ball.TouchEnded:Connect(OnTouchEndedFootball)
function M1Event(player,Ring,db,holding)
if hitObject then
ball:SetAttribute("Force", 27)
local character = hitObject.Parent
local ring = character:FindFirstChild("Ring")
local isDribbling = ring:GetAttribute("IsDribbling")
local camY = ring:GetAttribute("CameraY")
local camLV = ring:GetAttribute("Camera")
local YVector
if camY <= 11 then
YVector = 0.3
force = defaultForce * 1.6
elseif camY >= 12 then
YVector = 1.8
force = defaultForce * 1.8
end
local humanoid = character:FindFirstChild("Humanoid")
local humanoidRootPart = humanoid.RootPart
local direction
local moveDirection = humanoid.MoveDirection
ball.AssemblyLinearVelocity = Vector3.new(0,0,0)
ball.AssemblyAngularVelocity = Vector3.new(0,0,0)
print(moveDirection)
direction = (moveDirection) + Vector3.new(0,YVector,0)
ball:ApplyImpulse((direction * force) * ball.AssemblyMass)
ball:SetAttribute("LastPlayer",player.Name)
end
end
m1Event.OnServerEvent:Connect(M1Event)