Z4Zy Posted December 4, 2018 Share Posted December 4, 2018 Hello my friends ! I just trying to move an object away when someone fire on it. I created below client and server side codes. Client Side :- addEventHandler("onClientPlayerWeaponFire",root, function (_,_,_,_,_,_,h) if h and getElementType(h) == "vehicle" then triggerServerEvent("onFired",resourceRoot,h) end end ) Server Side :- addEvent("onFired",true) addEventHandler("onFired",root, function (h) setElementVelocity(h,0.1,0.1,0.1) end ) But the problem in here is, when fire in different directions, the velocity / force acted on the vehicle in same direction. Can you please help me to change the force direction of the vehicle according to the fire direction ? I thing maths should be applied in here [ may not :p ]. However, I'm really newbie in some of lua maths. [ math.sine, math.cos, math.tan, math.#%$...BLA.. BLA..]. So, at least little help will very useful !! Link to comment
Moderators IIYAMA Posted December 4, 2018 Moderators Share Posted December 4, 2018 (edited) Subtract the ped-target-start position from the ped-target-end postion(or hit position) https://wiki.multitheftauto.com/wiki/GetPedTargetStart https://wiki.multitheftauto.com/wiki/GetPedTargetEnd And then make a vector of it + normalize it: https://wiki.multitheftauto.com/wiki/Vector/Vector3#getNormalized If the vehicle is at a remote location, then use instead of the ped start position, the vehicle position and do the same steps. (You might want to consider using the hit location of the event instead of the ped-target-end position.) Edited December 4, 2018 by IIYAMA 1 Link to comment
Z4Zy Posted December 4, 2018 Author Share Posted December 4, 2018 Hey @IIYAMA !! I know you'll be here soon ! BTW, as I understand, I got the vehicle position as it is on remote location and ped-target-end position. Then subtract each other and normalize. Then passed to the server side as below. Client Side :- addEventHandler("onClientPlayerWeaponFire",root, function (_,_,_,_,_,_,h) if h and getElementType(h) == "vehicle" then local vehLocation = Vector3(getElementPosition(h)) local targetEnd = Vector3(getPedTargetEnd(localPlayer)) local dir = (vehLocation-targetEnd):getNormalized() triggerServerEvent("onFired",resourceRoot,h,dir.x,dir.y,dir.z) end end ) Server Side :- addEvent("onFired",true) addEventHandler("onFired",root, function (h,dirx,diry,dirz) setElementVelocity(h,0.1,0.1,0.1) end ) But I don't know how to use them to change vehicle's force direction. How to use them Link to comment
Moderators IIYAMA Posted December 4, 2018 Moderators Share Posted December 4, 2018 Ah sorry, in case of hitting the vehicle, you need to use ped target start. But you are almost there. 1 Link to comment
Z4Zy Posted December 4, 2018 Author Share Posted December 4, 2018 OMG I done it !! I done it !! It's working pretty much cool !! Thanx for your suppor @IIYAMA Client Side :- addEventHandler("onClientPlayerWeaponFire",root, function (_,_,_,_,_,_,h) if h and getElementType(h) == "vehicle" then local targetStart = Vector3(getPedTargetStart(localPlayer)) local targetEnd = Vector3(getPedTargetEnd(localPlayer)) local dir = (targetStart-targetEnd):getNormalized() triggerServerEvent("onFired",resourceRoot,h,dir.x,dir.y,dir.z) end end ) Server Side :- addEvent("onFired",true) addEventHandler("onFired",root, function (h,dirx,diry,dirz) setElementVelocity(h,-dirx,-diry,-dirz) end ) 1 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