Kovah85 10 Posted February 17, 2010 So I have a unit that is patrolling a random area, when the player shows up, I'd like the unit to run away from the player. I've tried a bunch of different things including allowFleeing and nothing seems to work right. What I think will work well is to create a waypoint far away in the opposite direction of the player when you're detected and set the unit to careless and it's speed to full. But here's my problem. I have no idea how to calculate the opposite direction of a player. Any help would be appreciated, I've tried a few things, and failed miserably. Share this post Link to post Share on other sites
TheHarvesteR 11 Posted February 17, 2010 (edited) Well, I don't know much about ArmA syntax (I just started playing around with it), but I figure what you could do is determine the direction from the one doing the fleeing to the one he's fleeing from, then turning it a full 180º and placing it a few hundred meters away on that direction. So, this could be done using a little trig, and determining the sin and cos of the angle between them (again, I don't really know the syntax, so all my advice is theoretical)... I believe you could extract the angle between north and this direction, and from that get the sin and cos... then invert both values and multiply your distance by cos to get an x value and by sin to get a y value. that should be your target. I'm gonna try and explain this in pseudocode var position_1 = runner position var position_2 = attacker position var angle = get angle from position 1 to position 2 in relation to north var cos = cos(angle) var sin = sin(angle) var targetX = position_1.x + (distanceToRun * -cos) var targetY = position_1.y + (distanceToRun * -sin) runner doMove [targetX, targetY, 0] //XYZ right? Now, if anyone could put that I just said into sqf ;) I hope this helps (or at least makes sense) Cheers Edited February 17, 2010 by TheHarvesteR Share this post Link to post Share on other sites
Kovah85 10 Posted February 17, 2010 Hey thanks a lot. I understand most of that and I'm going to try playing around with it to find a way to get the angle needed. Share this post Link to post Share on other sites
TheHarvesteR 11 Posted February 17, 2010 If I'm not mistaken, there is a function called atan2, which takes two parameters as x and y coordinates and returns the angle from that point in relation to zero, or takes two points and returns the angle between them... in Flash at least it's the former, so it might be necessary to get the delta position from both points, and then calculate the angle from that delta... kinda like this: var deltaX = pos1X - pos2X; var deltaY = pos1Y - pos2Y; var angle = atan2(deltaX, deltaY) Cheers Share this post Link to post Share on other sites
Kovah85 10 Posted February 17, 2010 (edited) Interesting, I'm going to try this out right now... I'll let you know how it works. EDIT: Wow! As far as I can tell, it's working. I'll post the code I used incase anyone else has a similar need. And TheHarvesteR, I definitely couldn't have figured it out without your help. Thank you. _tx = getPos Target select 0; _ty = getPos Target select 1; _px = getPos pOne select 0; _py = getPos pOne select 1; _deltaX = _px - _tx; _deltaY = _py - _ty; _angle = _deltaX atan2 _deltaY; _cos1 = cos(_angle); _sin1 = sin(_angle); _wpx = _px + (1000 * -_cos1); _wpy = _py + (1000 * -_sin1); _scared = 1; deleteWaypoint _wp; _fleePos = createMarker ["FleeTo", [_wpx, _wpy]]; Target doMove getMarkerPos _fleePos; Note: Target is obviously the name of the Target unit, and pOne is the name I assigned to the players unit. I hope someone else can find this useful. Edited February 17, 2010 by Kovah85 It works! Share this post Link to post Share on other sites