Jump to content

Basketball


Recommended Posts

Hi I am making last time the basketball resource. Here is the source:

If you think that you can help me just write here :)

170px-Basketball.png

Current Source:

  
  
local info = guiCreateLabel(1,1,1,1,"Press 'k' to pick up the ball.",true) 
  
inicjacja = 0 
  
addEventHandler("onClientRender",root, 
function(localPlayer) 
    for _,v in ipairs(getElementsByType 'object') do 
        if getElementModel(v) == 2114 then 
        local bX,bY,bZ = getElementPosition(v) 
        local mX,mY,mZ = getCameraMatrix( ) 
        local distance = getDistanceBetweenPoints3D(mX,mY,mZ,bX,bY,bZ) 
        local x,y = getScreenFromWorldPosition(bX,bY,bZ) 
        attached = getElementAttachedTo(v) 
        attachedElements = getAttachedElements ( v ) 
        if x and y then 
        guiSetPosition(info,x,y,false) 
        else 
        guiSetVisible(info,false) 
        end 
            if distance < 5 then 
                if inicjacja == 0 then 
                inicjacja = 1 
                guiSetVisible(info,true) 
                bindKey("k","down",play) 
                end 
            elseif distance > 5 then 
                if inicjacja == 1 then 
                inicjacja = 0 
                guiSetVisible(info,false) 
                unbindKey("k","down",play) 
                end 
            end 
        end 
    end 
end 
) 
  
picked = 0 
  
function play() 
for _,v in ipairs(getElementsByType 'object') do 
if getElementModel(v) == 2114 then 
    if picked == 0 and not isElementAttached(v) then 
    picked = 1 
    -- action when you have got the ball in your hands: 
    toggleAllControls(false) 
    bindKey("w","down",controls) 
    bindKey("w","up",controls) 
    bindKey("a","down",controls) 
    bindKey("a","up",controls) 
    bindKey("d","down",controls) 
    bindKey("d","up",controls) 
    bindKey("mouse1","down",controls) 
    bindKey("mouse2","down",controls) 
    bindKey("lshift","down",controls) 
    attachElements(v,localPlayer) 
    --setElementAttachedOffsets(attached,hX,hY,hZ,0,0,0) 
    guiSetVisible(info,false) 
    addEventHandler("onClientPreRender",root,rotationN) 
    elseif picked == 1 and isElementAttached(v) then 
    picked = 0 
    -- action when you haven't got the ball in your hands: 
    local bX,bY,bZ = getElementPosition(v) 
    toggleAllControls(true) 
    unbindKey("w","down",controls) 
    unbindKey("w","up",controls) 
    unbindKey("a","down",controls) 
    unbindKey("a","up",controls) 
    unbindKey("d","down",controls) 
    unbindKey("d","up",controls) 
    unbindKey("mouse1","down",controls) 
    unbindKey("mouse2","down",controls) 
    unbindKey("lshift","down",controls) 
    setPedAnimation(localPlayer,false) 
    detachElements(v) 
    setElementPosition(v,bX,bY,bZ-0.9) 
    --guiSetVisible(info,true) 
    removeEventHandler("onClientPreRender",root,rotationN) 
    end 
end 
end 
end 
  
function controls(key,state) 
    if attached then -- if he is attacking 
        -- offensive moves 
        if key == "w" and state == "down" then 
        setPedAnimation(localPlayer,"bsktball","BBALL_run",true,true,true) 
        elseif key == "w" and state == "up" then 
        setPedAnimation(localPlayer,"bsktball","BBALL_idleloop",true,true) 
        elseif key == "mouse1" and state == "down" then 
        setPedAnimation(localPlayer,"bsktball","BBALL_Jump_Shot",true,false) 
        hX,hY,hZ = getPedBonePosition(localPlayer,26) 
        moveObject(attached,1000,hX,hY,hZ) 
        end 
        -- defensive moves 
    elseif not attached then -- if he is defensing 
        if key == "a" and state == "down" then 
        setPedAnimation(localPlayer,"bsktball","BBALL_def_stepL",true,true) 
        elseif key == "a" and state == "up" then 
        setPedAnimation(localPlayer,"bsktball","BBALL_idleloop",true,true) 
        elseif key == "d" and state == "down" then 
        setPedAnimation(localPlayer,"bsktball","BBALL_def_stepR",true,true) 
        elseif key == "d" and state == "up" then 
        setPedAnimation(localPlayer,"bsktball","BBALL_idleloop",true,true) 
        end 
    end 
end 
  
function findRotation(x1,y1,x2,y2) 
  
  local t = -math.deg(math.atan2(x2-x1,y2-y1)) 
  if t < 0 then t = t + 360 end; 
  return t; 
  
end 
  
function rotationN() 
    local cx,cy,cz, lx, ly, lz = getCameraMatrix() 
    local x,y,z = getElementPosition(localPlayer) 
    setPedRotation(localPlayer, findRotation(x,y,cx,cy)+180 ); 
end 
  
function test() 
local pX,pY,pZ = getElementPosition(localPlayer) 
local gX,gY,gZ = getGroundPosition(pX,pY,pZ) 
ball = createObject(2114,pX,pY,pZ-0.9) 
end 
addCommandHandler("createball",test) 
  
  
function testremove() 
removeCommandHandler("createball",testremove) 
end 
addCommandHandler("removetest",testremove) 
  

I will update the resource this little thing soon.

If you have got any ideas just write here, you can change the source if you want

Currently I made only basketball attached to player there is no object dynamic

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...