• How to make my own car trunk
    8 replies, posted
Hello ! I would like to make my own car trunk system but I have one question :) How to know if I'm behind the vehicle (TDM car, LWCar, etc) when I press key IN_USE ? Because it's only that my real problem :D Do I need to create an invisible props who follow the vehicle when it move or can I make that more easily ? Thanks :) JackHemming
This is the function I use. The parameters are self-explanatory. [lua] function IsInFront( posA, posB, normal ) local Vec1 = ( posB - posA ):GetNormalized() return ( normal:Dot( Vec1 ) < 0 ) end [/lua] I think credit is due to Spencer for this.
[QUOTE=James xX;52554287]This is the function I use. The parameters are self-explanatory. [lua] function IsInFront( posA, posB, normal ) local Vec1 = ( posB - posA ):GetNormalized() return ( normal:Dot( Vec1 ) < 0 ) end [/lua] I think credit is due to Spencer for this.[/QUOTE] Oh ! Thanks you for your quick answer ! :) But.. I don't understand what are the arguments. I suppose that posA is the player:GetPos():Angle() and posB is player:GetEyeTrace().Entity:GetPos():Angle() but what is the normal argument ?
[QUOTE=JackHemming;52554323]Oh ! Thanks you for your quick answer ! :) But.. I don't understand what are the arguments. I suppose that posA is the player:GetPos():Angle() and posB is player:GetEyeTrace().Entity:GetPos():Angle() but what is the normal argument ?[/QUOTE] Angles are not positions. posA is the position of the car, posB is the position of the player, and normal is the forward vector of the car. for example, I use it like this (note, this is a very complicated example because what I used it for is different to what you are using it for) [lua] local pos = ( IsValid( viewent ) and viewent != LocalPlayer() ) and GetViewEntity():GetPos() or EyePos() if IsInFront( pos, self:GetPos(), self:GetForward() ) then -- stuff end [/lua] PosA and PosB are equivalent, but this needs to be taken into account in the normal. For example, if A is in front of B, B is behind A, and if B is behind A and the normal is flipped, B is in front of A.
[QUOTE=James xX;52554340]Angles are not positions. posA is the position of the car, posB is the position of the player, and normal is the forward vector of the car. for example, I use it like this (note, this is a very complicated example because what I used it for is different to what you are using it for) [lua] local pos = ( IsValid( viewent ) and viewent != LocalPlayer() ) and GetViewEntity():GetPos() or EyePos() if IsInFront( pos, self:GetPos(), self:GetForward() ) then -- stuff end [/lua] PosA and PosB are equivalent, but this needs to be taken into account in the normal. For example, if A is in front of B, B is behind A, and if B is behind A and the normal is flipped, B is in front of A.[/QUOTE] Ok ! Sorry ! I have some difficulty with vector and angles so I didn't understand :) Now, I use it like that : [lua] hook.Add("KeyPress", "open_car_trunk", function(player, key) if key == IN_USE then if player:GetEyeTrace().Entity:GetClass() == "prop_vehicle_jeep" then if IsInFront(player:GetEyeTraceNoCursor().Entity:GetPos(), player:GetPos(), player:GetForward()) then -- stuff end end end end) [/lua] But when I use the key IN_USE and that I'm targeting a vehicle, the function always return true. The problem is that I just want the function return true if I'm behind the vehicle (if I targeting the car trunk). Maybe it's impossible with this technique ? Thanks you for the time you take to help me :)
[QUOTE=JackHemming;52554437]Ok ! Sorry ! I have some difficulty with vector and angles so I didn't understand :) Now, I use it like that : [lua] hook.Add("KeyPress", "open_car_trunk", function(player, key) if key == IN_USE then if player:GetEyeTrace().Entity:GetClass() == "prop_vehicle_jeep" then if IsInFront(player:GetEyeTraceNoCursor().Entity:GetPos(), player:GetPos(), player:GetForward()) then -- stuff end end end end) [/lua] But when I use the key IN_USE and that I'm targeting a vehicle, the function always return true. The problem is that I just want the function return true if I'm behind the vehicle (if I targeting the car trunk). Maybe it's impossible with this technique ? Thanks you for the time you take to help me :)[/QUOTE] Don't use the forward vector of the player, use the forward vector of the car. Also a poor understanding of a technique doesn't mean its impossible to use for this. It's simple maths.
[QUOTE=James xX;52554456]Don't use the forward vector of the player, use the forward vector of the car. Also a poor understanding of a technique doesn't mean its impossible to use for this. It's simple maths.[/QUOTE] Hello ! Thanks a lot for your answer ! It helped me a lot ! :) I've done this and I think that is working : [lua] hook.Add("KeyPress", "open_car_trunk", function(player, key) if key == IN_USE then if player:GetEyeTrace().Entity:GetClass() == "prop_vehicle_jeep" then if !IsInFront(player:GetPos(), player:GetEyeTrace().Entity:GetPos(), player:GetEyeTrace().Entity:GetForward() - Vector(0,0,-80)) then print("true") else print("false") end end end end) [/lua] It tell me "true" only if I'm behind the vehicle and "false" if I'm in front or on the sides of the vehicle ! I wait before changing the status in "solved" because it's working but I don't know if it's correct or if there is a better solution (although this one is very easy !) Thanks again, again and again ! :)
The only other way to do it apart from what you're doing is to spawn a prop on the back of the car, which you probably don't want to do. That should be fine
[QUOTE=MPan1;52557773]The only other way to do it apart from what you're doing is to spawn a prop on the back of the car, which you probably don't want to do. That should be fine[/QUOTE] No, I don't want to do that :D It's good ! Thanks a lot guys ! :)
Sorry, you need to Log In to post a reply to this thread.