Sign in to follow this  
Thomas_Nightfire

NPC Driving Vehicle

Recommended Posts

Hello all!
Please help, I need to make a driving ped. So I've tried to create ped and create a vehicle and make ped drive to me, but soon I'm understood that I don't know how to make ped choose right direction (turning left or right) and go forward to me... Tried to investigate CrystalMV's NPC HLC scripts, but cannot understand how it works... So, please, somebody, help me code and understand how ur code works. I need a simple NPC turning into the right side and accelerate then stop using handbrake or brake_reverse and that's all. I've spend many hours thinking how to code it, but decision still not found, so I'm here..

Share this post


Link to post
Posted (edited)

Thomas_Nightfire
 

function getPointInFrontOfPoint(x, y, z, rZ, dist)
	local offsetRot = math.rad(rZ)
	local vx = x + dist * math.cos(offsetRot)
	local vy = y + dist * math.sin(offsetRot)  
	return vx, vy, z
end

function GetMarrot(angle, rz)
	local marrot = 0
	if(angle > rz) then
		marrot = -(angle-rz)
	else
		marrot = rz-angle
	end
	
	if(marrot > 180) then
		marrot = marrot-360
	elseif(marrot < -180) then
		marrot = marrot+360
	end
	return marrot
end

function findRotation(x1, y1, x2, y2) 
    local t = -math.deg(math.atan2(x2 - x1, y2 - y1))
    return t < 0 and t + 360 or t
end

local px,py,pz = getElementPosition(localPlayer)
local prx,pry,prz = getElementRotation(localPlayer)
local vx,vy,vz = getPointInFrontOfPoint(px,py,pz, prz+90, 15)
local thePed = createPed(299, vx,vy,vz+1, prz+180)
local theVehicle = createVehicle(404, vx,vy,vz+1, 0,0,prz+180)
warpPedIntoVehicle(thePed, theVehicle)

function UpdateBot()
	px,py,pz = getElementPosition(localPlayer)
	prx,pry,prz = getElementRotation(localPlayer)
	vx,vy,vz = getElementPosition(theVehicle)
	local vrx,vry,vrz = getElementRotation(theVehicle)
	local brakes = false
	local maxspd = 40
	local MaxDist = 4
	local vehreverse = false
	
	if(getDistanceBetweenPoints2D(px,py, vx, vy) < MaxDist) then
		brakes = true
	end
	
	if(brakes) then
		setPedAnalogControlState(thePed, "accelerate", 0)
		setPedAnalogControlState(thePed, "brake_reverse", 0)
		setPedControlState(thePed, "handbrake", true)
		setElementVelocity (theVehicle, 0,0,0)
	else
		local vxv, vyv, vzv = getElementVelocity(theVehicle)
		local s = (vxv^2 + vyv^2 + vzv^2)^(0.5)*156 -- Speed
		local rot = GetMarrot(findRotation(vx,vy,px,py),vrz)
    
		if(rot > 80) then 
			if(rot > 100) then vehreverse = true end
			rot = 20 
		elseif(rot < -20) then 
			if(rot < -80) then vehreverse = true end
			rot = -20 
		end
		
		if(vehreverse) then
			setPedAnalogControlState(thePed, "brake_reverse", 1-(s*1/maxspd))
			setPedAnalogControlState(thePed, "accelerate", 0)
			setPedControlState(thePed, "handbrake", false)
			if(s > 10) then
				setPedControlState(thePed, "handbrake", true)
			else
				if(rot > 0) then
					setPedAnalogControlState(thePed, "vehicle_left", (rot)/20)
				else
					setPedAnalogControlState(thePed, "vehicle_right", -(rot)/20)
				end
			end
		else
			if(rot > 0) then
				setPedAnalogControlState(thePed, "vehicle_right", (rot)/20)
			else
				setPedAnalogControlState(thePed, "vehicle_left", -(rot)/20)
			end
		
			setPedAnalogControlState(thePed, "brake_reverse", 0)
			setPedControlState(thePed, "handbrake", false)
			if(s < maxspd) then 
				setPedAnalogControlState(thePed, "accelerate", 1-(s*1/maxspd))
			else
				setPedAnalogControlState(thePed, "accelerate", 0)
				setPedAnalogControlState(thePed, "brake_reverse", (s/maxspd)-1)
			end
		end
	end
end
setTimer(UpdateBot, 50, 0)

Demo

Edited by alexaxel705
  • Like 1

Share this post


Link to post
Posted (edited)

Thank you very much! I'll try it now!

One question: what 'GetMarrot' function doing?

Edited by Thomas_Nightfire

Share this post


Link to post
Posted (edited)

Thomas_Nightfire

GetMarrot return Angle in the range from -180 to 180 on which we need to turn wheel.

-180 right, +180 left.

if active reverse +180 right, -180 left
function setPedAnalogControlState takes values from 0.0 to 1.0

therefore, we lower the angle concentration at an acceptable level

Edited by alexaxel705

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.