Jump to content

[HELP] Objects with collision in players.


Ryuto

Recommended Posts

Hello, thanks for entering my post.

Is it possible to add a function to an MTA object when interacting with a player?

I want to make an object (in this case a ball) explode when it touches a player.

I also wanted to ask if it is possible for it to explode upon contact with a wall or any other object on the map.

 

I leave a video showing my problem and what I want to achieve....

 

What functions could I use to achieve something similar?

 

Link to comment
2 hours ago, alex17" said:

you can use a createColCuboid attached to the object and the onClientColShapeHit event to detect when it collides with a player

 

use setDevelopmentMode and showcol to see the colshape

Thank you very much, I'm going to try it on my local server.

Is there a way for it to work with walls or other objects?

Link to comment
40 minutes ago, Ryuto said:

Thank you very much, I'm going to try it on my local server.

Is there a way for it to work with walls or other objects?

You can combine it with another method. When the object starts moving, use processLineOfSight to detect if it will collide with a wall, then calculate the time it will collide and create the explosion as long as the colshape has not been activated.

I'll leave you an example, I haven't tried it but let me know if it works for you or if it has any errors.

local object 
local colshape 
local timer 

function ExplosiveObject()

   object = createObject(...)  
   colshape = createColCuboid(...) 

   attachElements(object, colshape)

   -- Move the object to a specific position with time and rotation effects
   moveObject(object, time, fx, fy, fz, frx, fry, frz)

   -- Calculate line of sight between two points
   local hit, hx, hy, hz, elementHit = processLineOfSight(x, y, z, fx, fy, fz, true, true, false, true, true)

   -- Check if there was a collision and an element was hit
   if hit and elementHit then 
      -- If any coordinates are null, get the position of the hit element
      if not hx or not hy or not hz then 
         hx, hy, hz = getElementPosition(elementHit)
      end 
      
      -- Calculate the distance between two 3D points
      local dist1 = getDistanceBetweenPoints3D(x, y, z, fx, fy, fz)
      local dist2 = getDistanceBetweenPoints3D(x, y, z, hx, hy, hz)
      
      -- Calculate the time to create the explosion based on distances
      local time = (dist2 / dist1) * time
      
      -- Set a timer to create the explosion
      timer = setTimer(createExplosion_, time, 1)
   end 

   -- Add event handlers to detect when the colshape is hit
   addEventHandler("onClientColShapeHit", colshape, onClientColShapeHit)
   -- Add event handlers to detect when the object stops moving
   addEventHandler("onClientObjectMoveStop", object, onClientObjectMoveStop)
end 

-- Function to create an explosion at the position of the object
function createExplosion_()
   local x, y, z = getElementPosition(object)
   createExplosion(x, y, z, 0)  -- Create an explosion at position (x, y, z)
end 

-- Event handler for when the colshape is hit
function onClientColShapeHit()
   if timer and isTimer(timer) then 
      killTimer(timer)
   end 
   -- Call the function to create an explosion at the position of the object
   createExplosion_()
   -- Remove event handlers to prevent repetitions
   removeEventHandler("onClientColShapeHit", colshape, onClientColShapeHit)
   removeEventHandler("onClientObjectMoveStop", object, onClientObjectMoveStop)
end 

-- Event handler for when the object stops moving
function onClientObjectMoveStop() 
   if timer and isTimer(timer) then 
      killTimer(timer)
   end 
   -- Remove the event handler to prevent repetitions
   removeEventHandler("onClientObjectMoveStop", object, onClientObjectMoveStop)
end 

 

Edited by alex17"
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...