How to make my own car trunk

Hello !

I would like to make my own car trunk system but I have one question :slight_smile:

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 :smiley: Do I need to create an invisible props who follow the vehicle when it move or can I make that more easily ?

Thanks :slight_smile:

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.

Oh ! Thanks you for your quick answer ! :slight_smile:

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 ?

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.

Ok ! Sorry ! I have some difficulty with vector and angles so I didn’t understand :slight_smile:

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 :slight_smile:

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.

Hello ! Thanks a lot for your answer ! It helped me a lot ! :slight_smile:

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 ! :slight_smile:

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

No, I don’t want to do that :smiley:

It’s good ! Thanks a lot guys ! :slight_smile: