Chrimam Posted February 18, 2013 Share Posted February 18, 2013 (edited) How can i attach something to a ped's/player's weapon? I tried to attach a marker to a weapon so that wherever you aim, the marker will go there. I tried aimX, aimY, aimZ = getPedWeaponMuzzlePosition(getLocalPlayer()) but i think i made it wrong. and i don't know how to attach something to coordinates/weapon's muzzle position. Can you help me? function myWeaponMarker(weapon) if weapon == 24 then playerX, playerY, playerZ = getElementPosition(getLocalPlayer()) muzX, muzY, muzZ = getPedWeaponMuzzlePosition(getLocalPlayer()) weaponMarker = createMarker(playerX, playerY, playerZ, "corona", 0.5, 255, 0, 0, 170) ///this is where the attaching script will go. end end addEventHandler("onClientPlayerTarget", getRootElement(), myWeaponMarker) Edited February 18, 2013 by Guest Link to comment
Sora Posted February 18, 2013 Share Posted February 18, 2013 checkout wiki : https://wiki.multitheftauto.com/wiki/AttachElements This is not compatible with all elements. The following elements are compatible: Markers Blips Objects Players Vehicles Sounds Colshapes this means you can't attach a marker to weapon. Link to comment
Chrimam Posted February 18, 2013 Author Share Posted February 18, 2013 so what do you suggest? I tried getPedTarget but it doesn't work properly. Link to comment
Sora Posted February 18, 2013 Share Posted February 18, 2013 hmm this event will be handled when you face a player or aim him addEventHandler ( "onPlayerTarget",root, function (target) if getElementType (target) == "player" then -- if target is a player then local playerX, playerY, playerZ = getElementPosition(target) -- get his position weaponMarker = createMarker(playerX, playerY, playerZ, "corona", 0.5, 255, 0, 0, 170) -- create marker in his position attachElements(weaponMarker,target) -- attach the marker to the target end end) and this will be handled if you clicked the right button of mouse and checks the element etc.. addEventHandler("onPlayerClick",root, function (b,s) if p == "right" and s == "down" then -- if clicked button was right and buttons state is down then if getElementType (getPedTarget(source)) == "player" then -- if target is a player then local playerX, playerY, playerZ = getElementPosition(getPedTarget (source)) -- get his position weaponMarker = createMarker(playerX, playerY, playerZ, "corona", 0.5, 255, 0, 0, 170) -- create a marker in his position attachElements(weaponMarker,getPedTarget (source)) -- attach the marker to the target end end end) i hope this will help you i've wrote it quickly, forgive me if there's mistake in it Link to comment
Chrimam Posted February 18, 2013 Author Share Posted February 18, 2013 I don't want the marker to be attached to target. I want to attach the target in front of the weapon's muzzle position. and i think it's impossible for now. Link to comment
TAPL Posted February 18, 2013 Share Posted February 18, 2013 Possible with setElementPosition + onClientPreRender. Link to comment
Chrimam Posted February 19, 2013 Author Share Posted February 19, 2013 I don't get any errors but "badArgument @ destroyObject" which works fine for me. But anyways, I can't attach my marker to weapon (i mean set the marker's position everytime frame changes) function powerTeleport(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement) if weapon == 24 then playerX, playerY, playerZ = getElementPosition(getLocalPlayer()) rotX, rotY, rotZ = getElementRotation(getLocalPlayer()) teleportObject = createObject(1337, playerX, playerY, playerZ) setElementCollisionsEnabled(teleportObject, false) setElementAlpha(teleportObject, 0) attachElements(getLocalPlayer(), teleportObject, 0, 0, 0, 0, 0, 0) moveObject( teleportObject, 1000, hitX, hitY, hitZ, rotX, rotY, rotZ ) setTimer(destroyTeleportObject, 1100, 1) end end addEventHandler("onClientPlayerWeaponFire", getLocalPlayer(), powerTeleport) function destroyTeleportObject(teleportObject) detachElements(getLocalPlayer(), teleportObject) destroyElement(teleportObject) end function powerTeleportMarker(weapon, playerX, playerY, playerZ) if weapon == 24 then muzX, muzY, muzZ = getPedWeaponMuzzlePosition(getLocalPlayer()) teleportMarker = createMarker(0, 0, 10, "corona", 1, 255, 0, 0, 170) setElementPosition(teleportMarker, playerX, playerY, playerZ) addEventHandler("onClientPreRender", getRootElement(), function(teleportMarker, muzX, muzY, muzZ) setElementPosition(teleportMarker, muzX, muzY, muzZ) end) end end addEventHandler("onClientPlayerTarget", getRootElement(), powerTeleportMarker) Link to comment
Sora Posted February 20, 2013 Share Posted February 20, 2013 x,y,z = getElementPosition(getLocalPlayer()) weaponMarker = createMarker(x, y, z, "corona", 0.5, 255, 0, 0, 170) addEventHandler ( "onClientPreRender", root, function () if getPlayerWeapon(getLocalPlayer()) == 24 then muzX, muzY, muzZ = getPedWeaponMuzzlePosition(getLocalPlayer()) setElementPosition(weaponMarker,muzX, muzY, muzZ) end end) hmm i didn't try it but it should work Link to comment
Chrimam Posted February 20, 2013 Author Share Posted February 20, 2013 Uh it attaches marker to the weapon but i got a problem. whenever i look to an object (vehicles, peds or things added by scripts/maps), it creates a red marker on it (no it doesn't attach). and i don't want my marker to appear everytime i get desert eagle, i want it to appear when i aim. Hope you got the point (the red marker on my leg is the marker attached to the weapon) Link to comment
TAPL Posted February 20, 2013 Share Posted February 20, 2013 Try this: (Not Tested) addEventHandler("onClientPreRender", root, function() if getControlState("aim_weapon") and getPedWeapon(localPlayer) == 24 then local muzX, muzY, muzZ = getPedWeaponMuzzlePosition(localPlayer) if not isElement(weaponMarker) then weaponMarker = createMarker(muzX, muzY, muzZ, "corona", 0.5, 255, 0, 0, 170) end setElementPosition(weaponMarker, muzX, muzY, muzZ) else if isElement(weaponMarker) then destroyElement(weaponMarker) end end end) And, of course, you are the only who will see the marker when you aim (local player), other players won't see the marker. Link to comment
Chrimam Posted February 20, 2013 Author Share Posted February 20, 2013 Thank you guys it worked P.S: I know it's client side. I don't want anybody to see my marker it will interrupt them. My project is a little bit different 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