Jump to content
PH CWB

Sniper Missing Shot

Recommended Posts

So, I'm making this script to predict the target and then the bullet goes right on it. I'm recreating desert sniper shot from 2.4km away. But the problem is that the console of the editor is showing me this which will follow after the code below:
 

player addEventHandler ["Fired",
 { { player reveal _x } forEach allPlayers;
 _target = cursorTarget; 
if (!isNull _target) then { if (!isNull (vehicle _target)) then { _target = driver _target; };
 if (alive _target && _target isKindOf "Man" && group _target != group player) then { _bullet = _this select 6;
 _bulletVel = velocity _bullet;
 _targetPos = getPosATL _target; _targetVel = velocity _target;
 _targetDir = direction _target; _targetSpeed = _targetVel vectorMagnitude;
 _bulletSpeed = 1400; // Change to adjust the bullet speed
 _collisionTime = (_targetPos distance position _bullet) 
_bulletSpeed; _targetFuturePos = _targetPos vectorAdd (_targetVel vectorMultiply _collisionTime);
 _targetFuturePos = [_targetFuturePos select 0, _targetFuturePos select 1, (_targetFuturePos select 2) + 0.2]; // Add a small z offset to hit the body instead of the ground
 _bulletDir = _targetFuturePos vectorSubtract position
 _bullet; _bulletDir = _bulletDir vectorNormalized;
 _bulletVel = _bulletDir vectorMultiply _bulletSpeed;
 _bulletVel = _bulletVel vectorAdd (_targetDir vectorMultiply _targetSpeed);
 _bullet setVelocity _bulletVel; }; }; }];

 

Game console:
"...Speed = _targetVel |#|vectorMagnitude; _bullets..."
Error Missing ;

 

But I can't find where is it missing... I mean can it be asomething else?

Share this post


Link to post
Share on other sites

Thank you guys for pointing that out..I fixed it. 


here is the updated version but it still gives me another error and I double checked the syntax for this one so I don't know what might be causing the issue here:

 

Quote

player addEventHandler ["Fired", { { player reveal _x; } forEach allPlayers;
_target = cursorTarget; if (!isNull _target) then { if ((!isNull (vehicle _target))) then { _target = driver _target; };
if ((alive _target) && (_target isKindOf "Man") && (group _target != group player)) then { _bullet = _this select 6;
_bulletVel = velocity _bullet;
_targetPos = getPosATL _target;
_targetVel = velocity _target;
_targetDir = direction _target;
_targetSpeed = vectorMagnitude _targetVel;
_bulletSpeed = 1400; // Change to adjust the bullet speed
_collisionTime = (_targetPos distance position _bullet) / _bulletSpeed;
_targetFuturePos = _targetPos vectorAdd (_targetVel vectorMultiply _collisionTime);
_targetFuturePos = [_targetFuturePos select 0, _targetFuturePos select 1, (_targetFuturePos select 2) + 0.2]; // Add a small z offset to hit the body instead of the ground
_bulletDir = position _bullet vectorSubtract _targetFuturePos;
_bulletDir = _bulletDir vectorNormalized;
_bulletVel = _bulletDir vectorMultiply _bulletSpeed;
_bulletVel = _bulletVel vectorAdd (_targetDir vectorMultiply _targetSpeed);
_bullet setVelocity _bulletVel; }; }; }];
 


The error is with the vectorSubtract now.. 

Edit: the valid command is vectorDiff, sorry guys.

The issue is with this: _bulletDir vectorNormalized;

Share this post


Link to post
Share on other sites

Yeah I'm stuck with this line: _bulletDir = _bulletDir vectorNormalized;

I'm getting on console:
"bulletDir = _bulletDir  |#|vectorNormalized;."
Error Missing ;

Share this post


Link to post
Share on other sites

THank you Harzach I was indeed the syntax.

 

I can't figure out though why the below won't work:
1 - If the _bulletSpeed is a scalar single number, the vectorMultiply won't work; If it is an array it works. 
2 - However if it is an array either being a single number or three e.g: [100, 0 , 0]  I got error with the _collisionTime because it can't somehow divided.

Any light on this issue?
The error on editor is:
"Error /: type array, expected number, string, not a number". 
 

Quote

player addEventHandler ["Fired", {
  {
    player reveal _x;
  } forEach allPlayers;
  _target = cursorTarget;
  if (!isNull _target) then {
    if (!isNull (vehicle _target)) then {
      _target = driver _target;
    };
    if (alive _target && _target isKindOf "Man" && group _target != group player) then {
      _bullet = _this select 6;
      _bulletVel = velocity _bullet;
      _targetPos = getPosATL _target;
      _targetVel = velocity _target;
      _targetDir = direction _target;
      _targetSpeed = vectorMagnitude _targetVel;
      _bulletSpeed = [1422];
      _collisionTime = (_targetPos distance position _bullet) / _bulletSpeed select 0;
      _targetFuturePos = _targetPos vectorAdd (_targetVel vectorMultiply _collisionTime);
      _targetFuturePos = [_targetFuturePos select 0, _targetFuturePos select 1, (_targetFuturePos select 2) + 0.2];
      _bulletDir = _targetFuturePos vectorDiff position _bullet;
      _bulletDir = vectorNormalized _bulletDir;
      _bulletVel = _bulletDir vectorMultiply _bulletSpeed;
      _bulletVel = _bulletVel vectorAdd (_targetDir vectorMultiply _targetSpeed);
      _bullet setVelocity _bulletVel;
    };
  }; 
}];

 

Share this post


Link to post
Share on other sites
_bulletSpeed = [1422];

_bulletSpeed is an array with one element. vectorMultiply wants a scalar, so:

_bulletVel = _bulletDir vectorMultiply (_bulletSpeed # 0);

Or, since I can't see any reason why _bulletSpeed is an array, just do:

_bulletSpeed = 1422;

 

  • Like 2

Share this post


Link to post
Share on other sites
6 minutes ago, Harzach said:

_bulletSpeed = [1422];

_bulletSpeed is an array with one element. vectorMultiply wants a scalar, so:


_bulletVel = _bulletDir vectorMultiply (_bulletSpeed # 0);

Or, since I can't see any reason why _bulletSpeed is an array, just do:


_bulletSpeed = 1422;

 

I mean that's what I pointed out... if it is scalar, vectorMultiply won't work... it will tell me to do it as an array that's why it was as array before.. 

and so when I put it as an array I go the 2nd error above.

 

A first sight I thought it might have be with regarding as this to it perform a vector multiplication operation, both arrays must have the same length. in the code I'm trying to multiply an array of length 3 (the _bulletDir array) with an array of length 1 (the _bulletSpeed array). That is why I'm getting an error.

To fix this issue, I try to make sure that the _bulletSpeed array has a length of 3. SO I modify the _bulletSpeed array to have the same direction as the _bulletDir array, like this:
_bulletSpeed = [_bulletSpeed select 0, _bulletSpeed select 0, _bulletSpeed select 0];

But that didn't work either.

 

Share this post


Link to post
Share on other sites

You are reusing a few variables, which is not good practice. Sometimes it'll bite you. I can't do deep dive on this until later tonight or tomorrow, hopefully someone else can chime in...

  • Like 2

Share this post


Link to post
Share on other sites
38 minutes ago, Harzach said:

You are reusing a few variables, which is not good practice. Sometimes it'll bite you. I can't do deep dive on this until later tonight or tomorrow, hopefully someone else can chime in...


Well I changed the global to local variables. see below:

Even still, being array or scalar, won't work 😕

 

Quote

 

player addEventHandler ["Fired", {
  {
    player reveal _x;
  } forEach allPlayers;

  private ["_target", "_bullet", "_bulletVel", "_targetPos", "_targetVel", "_targetDir", "_targetSpeed", "_bulletSpeed", "_collisionTime", "_targetFuturePos", "_bulletDir", "_bulletVel"];

  _target = cursorTarget;
  if (!isNull _target) then {
    if (!isNull (vehicle _target)) then {
      _target = driver _target;
    };
    if (alive _target && _target isKindOf "Man" && group _target != group player) then {
      _bullet = _this select 6;
      _bulletVel = velocity _bullet;
      _targetPos = getPosATL _target;
      _targetVel = velocity _target;
      _targetDir = direction _target;
      _targetSpeed = vectorMagnitude _targetVel;
      _bulletSpeed = 1422;
      _collisionTime = (_targetPos distance position _bullet) / _bulletSpeed;
      _targetFuturePos = _targetPos vectorAdd (_targetVel vectorMultiply _collisionTime);
      _targetFuturePos = [_targetFuturePos select 0, _targetFuturePos select 1, (_targetFuturePos select 2) + 0.2];
      _bulletDir = _targetFuturePos vectorDiff position _bullet;
      _bulletDir = vectorNormalized _bulletDir;
      _bulletVel = _bulletDir vectorMultiply _bulletSpeed;
      _bulletVel = _bulletVel vectorAdd (_targetDir vectorMultiply _targetSpeed);
      _bullet setVelocity _bulletVel;
    };
  };

}];

 


 

Share this post


Link to post
Share on other sites

I can't really spot the problem with your code (at least at the line of interest). Furthermore you suggest that the same command works as intended 4 lines above the one with the issue.

 

Maybe you would like to try the following, which produces the same result for a scalar-vector multiplication and see if you get the same or a similar error.

 

_bulletVel = _bulletDir apply {_x * _bulletSpeed};

Something else you could (and possibly should) check is that the involved parameters are indeed what you expect them to be. _bulletDir should be a 3-element array (3D vector) and _bulletSpeed is indeed a scalar. I am not sure why you get the error you get but we're gonna find out together :).

 

Alternatively, you could get the values of the _bulletDir and _bulletSpeed and try to use the command in a different, isolated scope or even a different .sqf file and see if you get the same (or a similar) error.

Share this post


Link to post
Share on other sites

Thank you, Pierre. and Thank all of you that took some of your time to answer me.


I Tried everything I could with all of your answer but I couldn't. 

 

Thank you for your help though.. it didn't work. I'm abandoning the project. 

 

Quote

player addEventHandler ["Fired", {
  {
    player reveal _x;
  } forEach allPlayers;

  private ["_target", "_bullet", "_bulletVel", "_targetPos", "_targetVel", "_targetDir", "_targetSpeed", "_bulletSpeed", "_collisionTime", "_targetFuturePos", "_bulletDir", "_bulletVel"];

  _target = cursorTarget;
  if (!isNull _target) then {
    if (!isNull (vehicle _target)) then {
      _target = driver _target;
    };
    if (alive _target && _target isKindOf "Man" && group _target != group player) then {
      _bullet = _this select 6;
      _bulletVel = velocity _bullet;
      _targetPos = getPosATL _target;
      _targetVel = velocity _target;
      _targetDir = direction _target;
      _targetSpeed = vectorMagnitude _targetVel;
      _bulletSpeed = 1422;
      _collisionTime = (_targetPos distance position _bullet) / _bulletSpeed;
      _targetFuturePos = _targetPos vectorAdd (_targetVel vectorMultiply _collisionTime);
      _targetFuturePos = [_targetFuturePos select 0, _targetFuturePos select 1, (_targetFuturePos select 2) + 0.2];
      _bulletDir = _targetFuturePos vectorDiff position _bullet;
      _bulletDir = vectorNormalized _bulletDir;
      _bulletSpeed = _bulletSpeed * 0.5 + (_bulletSpeed * 0.5) * random 0.1;
      _bulletVel = [_bulletDir select 0 * _bulletSpeed, _bulletDir select 1 * _bulletSpeed, _bulletDir select 2 * _bulletSpeed];
      _bulletVel = _bulletVel vectorAdd [0,0,-9.81 * 0.05];
      _bullet setVelocity _bulletVel;
    };
  };
}];


Can close the topic now.

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

×