Ryuto Posted September 18, 2023 Share Posted September 18, 2023 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
alex17" Posted September 18, 2023 Share Posted September 18, 2023 (edited) 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 Edited September 18, 2023 by alex17" Link to comment
Ryuto Posted September 19, 2023 Author Share Posted September 19, 2023 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
alex17" Posted September 19, 2023 Share Posted September 19, 2023 (edited) 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 September 19, 2023 by alex17" Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now