Hiya, ive been trying to code a system that detects when a player is surfing. So far, i haven't gotten very far. Basically detects if the world is on the immediate right or left of a player, although i haven't figured out how to make it do that very well. It only works about 80% of the time.
Can anyone spot some mistakes and point out how i can fix them?
The point of my work is to add a kind of grind effect while surfing.
[lua]
function ply:OnRamp() // Is the player on a ramp? VERY HACKY CODE.
local pos = self:GetAngles():Forward()
local surftraceleft = {}
surftraceleft.start = self:GetPos() + Vector(0,0,-1)
surftraceleft.endpos = pos + (self:GetPos() + Vector(-60,0,0))
local surftraceright = {}
surftraceright.start = self:GetPos() + Vector(0,0,-1)
surftraceright.endpos = pos + (self:GetPos() + Vector(60,0,0))
local surftracefw = {}
surftracefw.start = self:GetPos() + Vector(0,0,-1)
surftracefw.endpos = pos + (self:GetPos() + Vector(0,60,0))
local surftracebw = {}
surftracebw.start = self:GetPos() + Vector(0,0,-1)
surftracebw.endpos = pos + (self:GetPos() + Vector(0,-60,0))
local trl = util.TraceHull( surftraceleft )
local trr = util.TraceHull( surftraceright )
local trf = util.TraceHull( surftracefw)
local trb = util.TraceHull( surftracebw )
if (trl.HitWorld or trr.HitWorld or trf.HitWorld or trb.HitWorld) and !self:OnGround() then
return true
elseif !(trl.HitWorld or trr.HitWorld or trf.HitWorld or trb.HitWorld) or self:OnGround() then
return false
end
end
[/lua]
Elsewhere there's arguments that gauge speed to determine if the player is surfing and not just walking.
Thanks in advance!
[i]snip didn't read[/i]
Perhaps doing a couple diagonal traces and getting the normal of the surfaces, combined with speed, would give a more accurate result.
I'd probably just do a check that'd look like this (I can't be bothered writing a code atm because it's 4AM.)
[img]http://puu.sh/4No6U.png[/img] - The green being diagonal traces. And depending on how tricky you want to be, you could do some funky sin,cos,tan expression to do a triangular trace like the yellow lines?
This is all great, but i'm sorry to report that im not the best at math. If someone is able to, could you kindly write out the lua and explain whats going on with it?
I'm babby mode with advanced math in lua.
And, all its doing is checking if you're near a wall and at a specific speed to be more or less approximate.
Since OnGround returns false when surfing, all i have to do is make sure they're next to a wall, going fast, and not on the ground.
Why is it that i need to go towards more advanced math again?
The maths it's self is easy. Trace downwards, get the normal to the plane of intersection, and get angle between vectors. Then just check if the speed is greater than running/walking speed (only in x and y plane to prevent falling being detected as surfing).
The Lua is not much more complicated than that. 50 lines of nicely spaced code should do the trick in a move hook.
[QUOTE=dingusnin;42490322]The maths it's self is easy. Trace downwards, get the normal to the plane of intersection, and get angle between vectors. Then just check if the speed is greater than running/walking speed (only in x and y plane to prevent falling being detected as surfing).
The Lua is not much more complicated than that. 50 lines of nicely spaced code should do the trick in a move hook.[/QUOTE]
so basically i'm measuring the wall, seeing if its correct?
[editline]11th October 2013[/editline]
[QUOTE=Minteh Fresh;42489549]I'd probably just do a check that'd look like this (I can't be bothered writing a code atm because it's 4AM.)
[img]http://puu.sh/4No6U.png[/img] - The green being diagonal traces. And depending on how tricky you want to be, you could do some funky sin,cos,tan expression to do a triangular trace like the yellow lines?[/QUOTE]
i just now got what you were saying with this.
In any case, how can i do a trace based on the direction of the player?
[QUOTE=Archemyde;42491463]so basically i'm measuring the wall, seeing if its correct?
[/QUOTE]
No.
1. Do a trace downwards using util.TraceLine [URL]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index9ba0.html[/URL] . Put the playerpos as startpos, then endpos as playerpos - Vector(0,0,x) where x is some value you need to figure out being how close the player is to the surface below while surfing.
2. Grab the traceresult, grab .HitNormal from it. If the trace didn't hit, the player is not on the ramp and we can end.
3. Do a dot product between the hitnormal and Vector(0,0,1) (vertical line facing up). The result you get is how steep the slope is, 1 means its horisontal, 0 means its vertical.
4. Find out how steep a slope needs to be to be surfable, based on the dotproduct.
5. Make sure the dotproduct is within the steepness.
6. Make sure the player has a high enough speed.
There, that's basically it. If you need even more in depth, ask again. :)
Sorry, you need to Log In to post a reply to this thread.