Jump to content

[¡HELP!] Detect Collision


Lalalu

Recommended Posts

Hello everyone, I need your help with something...

I'm gonna show you a simple code (server-side) that creates a moving object with moveObject;

  
    function moveObjectTest(player)
        local x, y, z = getElementPosition (player)		
		local r = getPedRotation(player) 		
        local object = createObject(8417, x+math.sin(math.rad(-r))*(1.5),y+math.cos(math.rad(-r))*(1.5),z-0.2, 0, 0, r)
		moveObject (object,700,x+math.sin(math.rad(-r))*25,y+math.cos(math.rad(-r))*25,z-0.3,0,50,0)
     end
  addCommandHandler ("move", moveObjectTest) 

I want to know how can I stop the object movement only when It hits Walls or Peds instead of using timer or x - y position, I mean,  by detecting the impact with other collision and then the object stop moving (sorry for my english?)

 

Edited by Lalalu
Link to comment
  • Lalalu changed the title to [¡HELP!] Detect Collision
    local data = {}
    
    function moveObjectTest(player)
        local x, y, z = getElementPosition (player)		
		local r = getPedRotation(player) 		
        data.object = createObject(8417, x+math.sin(math.rad(-r))*(1.5),y+math.cos(math.rad(-r))*(1.5),z-0.2, 0, 0, r)
        data.marker = Marker(x, y, z, "corona", 1, 255, 255, 255, 0)
        data.timer  = setTimer(onObjectStop, 700, 1)
        attachElements(marker, object)
        addEventHandler("onMarkerHit", data.marker, collision)
        moveObject (object,700,x+math.sin(math.rad(-r))*25,y+math.cos(math.rad(-r))*25,z-0.3,0,50,0)

     end
  addCommandHandler ("move", moveObjectTest) 

  function collision(element, samedim)
     if dimension then 
        removeEventHandler("onMarkerHit", data.marker, collision)
        killTimer(data.timer)
        destroyElement(data.marker)
        stopObject ( data.object )
     end 
  end
  
  function onObjectStop()
      removeEventHandler("onMarkerHit", data.marker, collision)
      destroyElement(data.marker)
  end 

you can use a marker to detect elements colliding with the object

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