Jump to content

SetVelocity according to the direction.


Z4Zy

Recommended Posts

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

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 by IIYAMA
  • Thanks 1
Link to comment

Hey @IIYAMA !!

           I know you'll be here soon :D !

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

OMG I done it !! I done it !! :lol:

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
)

 

  • Like 1
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...