Jump to content
Sign in to follow this  
punkatux

[script] - Moving target

Recommended Posts

Hello!

I post here the script I made which is useful and easy to use when you want to make some kind of target to move smoothly between 2 defined positions. Feel free to post any bug-report here. If you use the script, please keep the header unmodified. The header is also filled with informations how to use the script, so check there.

While I saw some similair scripts around, this one is written in performance-friendly way and should be independent of server FPS.

move_object.sqf

/*
Author:  punkatux
e-mail:  punkatux@gmail.com
version: 0.1

This script moves object smoothly between two markers with either constant or varying speed.

Parameters: [object, marker_start, marker_end, fps, speed_min, speed_max]
	object 		- object that will move between markers
	marker_start 	- marker placed at initial position
	marker_end	- marker placed at target position
	fps		- wanted FPS, must be > 0
	speed_min	- minimun speed of object in m/s
	speed_max	- maximum speed of object in m/s
			- speed_max is optional parameter, in case it's missing, target moves with constant speed of speed_min

For constant speed, use this - 2 m/s:
	[this, "marker_start", "marker_end", 100, 2] execVM "move_object.sqf";

For speed in some range, use this - 2~5 m/s:
	[this, "marker_start", "marker_end", 100, 2, 5] execVM "move_object.sqf";
*/

_object 	= _this select 0;
_marker_start	= _this select 1;
_marker_end	= _this select 2;
_fps 		= _this select 3;
_speed_min 	= _this select 4;
_speed_max 	= _this select 5;

if(isNil("_speed_max")) then {_speed_max = _speed_min};
if(_speed_max < _speed_min) then {_temp = _speed_max; _speed_max = _speed_min; _speed_min = _temp};

_frame_len = 1.0/_fps;

_marker_start_pos = getMarkerPos _marker_start;
_marker_end_pos   = getMarkerPos _marker_end;

_marker_start_x = _marker_start_pos select 0;
_marker_start_y = _marker_start_pos select 1;
_marker_end_x   = _marker_end_pos   select 0;
_marker_end_y   = _marker_end_pos   select 1;

_speed_diff = _speed_max - _speed_min;
_distance   = _marker_start_pos distance _marker_end_pos;
_direction  = (_marker_end_x - _marker_start_x) atan2 (_marker_end_y - _marker_start_y);

_start = time;
_end = _start;

_speed = _speed_min;

_distance_now = 0;
_object setPos _marker_start_pos;

while {true} do {
_delta_t = _end - _start;
_start = _end;

if(_speed_diff > 0) then {
	_speed = _speed - 0.25 + random 0.5;
	if(_speed < 0) then {
		if(_speed < -_speed_max) then {_speed = -_speed_max};
		if(_speed > -_speed_min) then {_speed = -_speed_min};
	};
	if(_speed > 0) then {
		if(_speed < _speed_min) then {_speed = _speed_min};
		if(_speed > _speed_max) then {_speed = _speed_max};
	};
};

if(_distance_now < 0) then {
	_object setPos _marker_start_pos;
	_distance_now = 0;
	_speed = -_speed;
};
if(_distance_now > _distance) then {
	_object setPos _marker_end_pos;
	_distance_now = _distance;
	_speed = -_speed;
};

_distance_now = _distance_now + _delta_t * _speed;

_x = _marker_start_x + sin(_direction) * _distance_now;
_y = _marker_start_y + cos(_direction) * _distance_now;
_object setPos [_x,_y, 0];

_time_reserve = _frame_len - _delta_t;
if(_time_reserve > 0) then {
	sleep _time_reserve;
};

_end = time;
};

---------- Post added at 12:36 ---------- Previous post was at 12:31 ----------

Damn, put that in configs & scripting section, instead of mission-making & scripting. Can I fix it somehow, or can any admin do so?

Share this post


Link to post
Share on other sites

The script work fine in local editor, but in a server the target popup move to jump, to jerky. any solution?

sorry for my english, i'm italian

Thx

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×