Jump to content

[Help] Car Ball Testing


Recommended Posts

  • 2 months later...
  • 4 months later...

ok whats problem here or how let it falls ??

-- client --

addEvent("onCarballPosUpdate", true)
addEvent("onClientVehicleCollision", true)

local hit_ball = function(hitElement, force)
	if(isPedInVehicle(localPlayer)) then
		setVehicleDamageProof(getPedOccupiedVehicle(localPlayer), true)
	if(hitElement) and (getElementDimension(hitElement) == getElementDimension(localPlayer)) then
		if(isBall(hitElement)) then
			local x, y, z = getElementPosition(hitElement)
			local throw = false
			local s = playSound3D("kick.mp3", x, y, z, false)
			setElementDimension(s, getElementDimension(hitElement))
			setSoundMaxDistance(s, 100)
			setSoundVolume(s, 0.5)
			x, y, z = getElementVelocity(hitElement)
			if(force > 500) then
				throw = true
			if(force > 100) then
				force = 100
			local nx, ny, nz = x*(force/30), y*(force/30), z+((force/300)*1.01)
			if(source == getPedOccupiedVehicle(localPlayer)) then
				x, y, z = getElementPosition(hitElement)
				triggerServerEvent("onCarballBallUpdate", localPlayer, hitElement, x, y, z, nx, ny, nz)
				if(throw == true) then
			setElementVelocity(hitElement, nx, ny, nz)
addEventHandler("onClientVehicleCollision", getRootElement(), hit_ball)

isBall = function(ball)
	return getElementData(ball, "ball")

local sync_ball = function(last_syncher, ball, x, y, z, x2, y2, z2)
	if(last_syncher ~= localPlayer) then
setElementPosition(ball, x, y, z)
setElementVelocity(ball, x2, y2, z2)
addEventHandler("onCarballPosUpdate", getRootElement(), sync_ball)


-- server --

Ball = {}

if isElement(Ball[player]) then return outputChatBox('u have one', source) end
local x,y,z = getElementPosition(player)
Ball[player] = createObject(2912, x + 3, y, z)
outputChatBox('done', player)
setElementData(Ball[player], 'ball', true)

local carball_update = function(ball, x, y, z, x2, y2, z2)
triggerClientEvent("onCarballPosUpdate", getRootElement(), source, ball, x, y, z, x2, y2, z2)
addEventHandler("onCarballBallUpdate", getRootElement(), carball_update)


any one help me resource 97% completely :| 

Link to comment
7 hours ago, MisterQuestions said:

I don't think that will even set a direction to ball

how to get that ??

and how to apply physics to ball or object ??

if there was functions or what give me pls ??

and i want learn can some one help me ??

or give me code and help me pls

Link to comment
17 hours ago, MisterQuestions said:

I did the same mistake as you time ago.


  1. -- "ball" is the ball element
  2. -- "vehicle" is the vehicle element that collided with the ball
  4. local x, y, z = getElementPosition(ball)
  5. local x2, y2, z2 = getElementPosition(vehicle)
  6. local velX, velY, velZ = getElementVelocity(vehicle)
  7. local vel = math.sqrt(velX*velX + velY*velY + velZ*velZ) -- equal to getDistanceBetweenPoints3D(0, 0, 0, velX, velY, velZ)
  9. local dirX, dirY, dirZ = x - x2, y - y2, z - z2
  10. local dist = math.sqrt(dirX*dirX + dirY*dirY + dirZ*dirZ) -- equal to getDistanceBetweenPoints3D(x, y, z, x2, y2, z2)
  11. dirX, dirY, dirZ = dirX / dist, dirY / dist, dirZ / dist
  13. -- To change velocity of the ball now, i guess you might want to do:
  14. -- setElementVelocity(ball, dirX * vel, dirY * vel, dirZ * vel)

thats work ??

  • Sad 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...