How to calculate the player character direction where its facing?

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)

Hrp.Velocity.Unit? This is essentially the same thing as MoveDirection. Maybe explain the goal a little more.

I will also add that your issue might not revolve around the direction but rather it seems to be how its updated. Just by looking at the video I can tell its not the velocity being the problem.

Ah the reason this happens is because you are doing this on the server. The server updates as fast as your internet speed. You can fix the issue by doing this on the client instead of the server since the client will update faster and you can still use lookvector.

1 Like

ok guys i will try to get the look vector on the client side and pass it on to the server if that’s what meant to do.

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)
        
        -- Use the orientation of the HumanoidRootPart if the character is not moving
        if humanoid.MoveDirection.Magnitude == 0 then
            direction = -(humanoidRootPart.CFrame.LookVector)
        else
            direction = moveDirection
        end
        direction = direction + Vector3.new(0,YVector,0)

		ball:ApplyImpulse((direction * force) * ball.AssemblyMass)

		ball:SetAttribute("LastPlayer",player

Thankyou so much, it worked out!

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