Vehicle direction using GetVelocity

Hi Facepunch,

I’m trying to find a way to detect the direction the vehicle is going… if it’s going forward or backward.



local function VehicleDirection(ply)

     local EntVehicle = ply:GetVehicle()
    
     local Speed = EntVehicle:GetVelocity():Length() 

end


Hope someone can help!

Thanks

Take a look at my skletonized dev base: https://bitbucket.org/Acecool/acecooldev_base/overview

I’m working on moving a lot of the helper functions over; just saw this and already have one set up: https://bitbucket.org/Acecool/acecooldev_base/raw/master/gamemode/shared/entity/entity_getmovedirection.lua?at=master

I do have an “advanced” version which will compare and return many different directions; but I uploaded the simplified for you…

Feel free to add me on Steam; I tutor quite a few people.

This is fantastic! It’s exactly what I’m looking for. Thank you so much.

Btw…I REALLY like your code style.

Changed it around a bit and added the vehicle speed difference based on units as idling non-handbraked vehicle will scoot 2.5-3ish units. so 4 seems fine. Other objects won’t have that restriction. Also change the code around a bit so Dot is only called if needed. If not moving, returns right away, then tests forward, back, left right…

If you copy/paste, you’ll need the META_ENTITY reference:
https://bitbucket.org/Acecool/acecooldev_base/raw/master/gamemode/shared/_definitions/_metatables.lua?at=master

As said, feel free to add me on Steam. I’ve got 400+ tutorials / systems that I share with people I tutor ( free ). Additionally the dev base is continually growing…

Some other interesting things found in the useful programs file.

Hey, welcome to FacePunch.
I have written over 400 tutorials and completed “systems” in Lua for Garry’s Mod. I tutor and answer questions for free; feel free to add me on Steam if you need some guidance. This forum is for devs that need help working on things. Here are some resources to help you get started:

Generalized Lua Help ( Links to Wikis, Answers the question of “Where do I post a simple question or DarkRP Specific question”, links to other resources compiled by forum members )
https://dl.dropboxusercontent.com/u/26074909/tutoring/___welcome_docs/_welcome_general_lua_learning.lua.html

Useful Programs ( SteamCMD, Autosizer, Desktops, Process Explorer ) and Notepad++ Upgrades
https://dl.dropboxusercontent.com/u/26074909/tutoring/___welcome_docs/_welcome_useful_programs_and_notepadpp_upgrades.lua.html

AcecoolDev_Base Skeletonized Dev Base Game-Mode ( Never worry about Include or AddCSLuaFile ever again; comes with New Hooks, Console Commands, Meta-Table Objects, Helper Functions, Extended Functionality, and more! )
https://dl.dropboxusercontent.com/u/26074909/tutoring/___welcome_docs/_welcome_acecooldev_base_gamemode_info.lua.html