Errr... Lets see. I've coded this a long time ago, but I think it is not difficult at all.
We need two things:
- a function that checks the direction that the car should take to run over the unit
- a loop that sets the velocity of the car according to the direction
Errr....
_unit = _this select 1
_car = _this select 0
;This is the distance when the car is going to be forced to crash with the unit
_dis = 20
_getRelDir = { private ["_unit","_car"]; _car = _this select 0; _unit = _this select 1; ((position _unit select 1)-(position _car select 1)) atan2 ((position _unit select 0)-(position _car select 0)) }
;Wait until the car is _dis meters close to the unit
@_unit distance _car < _dis
;velocity in m/s: 20 is 72Kmh, which is quite fast!
;_vel = (speed _car)/3.6
_vel = 20
#setVel
_car setVelocity [_vel*(sin ([_car,_unit] call _getRelDir)),_vel*(cos ([_car,_unit] call _getRelDir)),velocity _car select 2]
~0
? _unit distance _car > 1: goto "setVel"
exit
OK. I think it would work... Of course, I don't have OFP here to test it...
Anyway, the possible bugs are:
#_getRelDir (get relative direction, in other words, it is the declivity of the line that unites both positions) might have switched coordinates.
A fast and reliable way of debugging it, is making an unit's direction to the value returned and check if it is looking at the other unit.
E.g.:
MyUnit setDir ([MyUnit,player] call { private ["_unit","_car"]; _car = _this select 0; _unit = _this select 1; ((position _unit select 1)-(position _car select 1)) atan2 ((position _unit select 0)-(position _car select 0)) })
Place the code in the editor and then check if the unit is looking at you.
If not, try switching the arguments - [MyUnit,player] to [player,MyUnit]
#Car is too fast or it accelerates too much instantaneously...
Well, this bug is due to the _vel variable defined in the script. If you don't want to set your own velocity for the car, uncomment the line "_vel = (speed _car)/3.6" and remove/comment the next one. It will check the current velocity of the car and then will force it to drive at that velocity.
#Car is approaching the unit in the right direction, but it is facing another direction...
Sometimes, the vehicles (often choppers) will head a different direction if they're forced to move along an user-defined direction. That happens because they want to stop - as if they had their own personality ;D.
In order to avoid it, add the following line to the loop:
_car setDir ([_car,_unit] call _getRelDir)
#Unit doesn't get killed after being run over
It doesn't?? Damn...
EhEhEh!