Not really a ‘how-to’ or tutorial, but more so a useful snippet for many I suppose, which is why I decided to share it.
In needed something working effectively to detect the upward vector of an object (also when it has been ‘tipped over’), and was struggling a while with re-calculating angles just to discover that some objects will give issues when doing so for what I needed.
Like most (i assume) i know about GetEntityForwardVector and how to place an object in front of the player for example with it. I however noticed that the other two functions (GetEntityUpVector and GetEntityRightVector) aren’t “exposed” within FiveM.
So I decided to do a bit of digging and came up with the following two functions:
function XNL_GetEntityUpVector(Entity) local ForwardVector, RightVector, UpVector, PosVector = GetEntityMatrix(Entity) return UpVector end function XNL_GetEntityRightVector(Entity) local ForwardVector, RightVector, UpVector, PosVector = GetEntityMatrix(Entity) return RightVector end
I on purpose put my commonly used _XNL prefix infront of these functions just in case that these functions/callbacks might be added to FiveM in the future (so that these won’t cause a conflict then).
These two can be used the exact same way as GetEntityForwardVector.
RegisterCommand("TestUpVector", function(source, args) local PlayerCords = GetEntityCoords(GetPlayerPed(-1)) UpVec = XNL_GetEntityUpVector(GetPlayerPed(-1)) local NewXup, NewYup, NewZup = table.unpack(PlayerCords + UpVec * 3.0) -- Creating New coordinates '3 meters' above the player AddExplosion(NewXup, NewYup, NewZup, 22, 1.0, true, false, 0.0) -- Adds Flare end)
Just paste the above sample code in an existing CLIENT lua script to test it.
OBVIOUSLY it’s not difficult to spawn items above the player by just using getting it’s coordinates and then adding to the Z coordinate, however what I needed it for wasn’t going to work this way.
I can’t (and won’t) spoil yet what I needed it for (I’m planning on releasing it in the near future though), but I can assure you that It does work for detecting the UpVector for objects which are spawned by script and then tipped/kicked/pushed over… With all the tests we have done it will indeed spawn our test above our ‘fallen over object’. Which means that if the object was laying tipped over on the floor it would also spawn our test object above it laying on the floor… And if it was just standing up, it would spawn it floating above it in the air…
And that was exactly what I/we needed. So long story I know, but I’m hoping this text also elaborates the usefulness of functions like these for beginners
Q: Can I use this in my code/Project
A: Yeah, duh sure it’s just a few simple lines.
Q: Do I need to give credits when I use these snippets?
A: For those couple of lines? Hell no
Q: Can I use these in my Paid content?
A: While I’m usually against using my FREE code in paid content by others (because I personally feel like we should just help each other /others by advancing in development techniques by sharing sources and just supporting by donations when we find it appropiate), These lines are WAY to simple and not even worth it to “restrict” or “copyright” So be my guest, I hope it helped atleast one other person besides myself that I’ve “written” those two functions.
So if you like the code or it was useful to use atleast click the ’ ’ to show some appreciation and to make others aware of these two simple functions
Happy Coding You all,