Difference between revisions of "Running Scripts On Arrows"
m
no edit summary
imported>OycT2h m |
imported>Tekuromoto m |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 102: | Line 102: | ||
set pxRot to player.getAngle x ; get player's vertical facing | set pxRot to player.getAngle x ; get player's vertical facing | ||
set pxRot to ( pxRot / -1.5 ) | set pxRot to ( pxRot / -1.5 ) | ||
set zOffset to ( zOffset | set zOffset to ( zOffset + pxRot ) | ||
if ( player.getDistance taTrigNoisemakerRef < 200 ) ; trigZone is in same cell as player | if ( player.getDistance taTrigNoisemakerRef < 200 ) ; trigZone is in same cell as player | ||
Line 108: | Line 108: | ||
set yp to player.getPos y ; store player's coordinates | set yp to player.getPos y ; store player's coordinates | ||
set zp to player.getPos z | set zp to player.getPos z | ||
set zp to ( zp | set zp to ( zp + zOffset ) ; adjust for zOffset | ||
acTrigZoneRef.setpos x xp | acTrigZoneRef.setpos x xp | ||
acTrigZoneRef.setpos y yp ; and move trigZone to those coordinates | acTrigZoneRef.setpos y yp ; and move trigZone to those coordinates | ||
Line 234: | Line 234: | ||
set zp to trigRef.getPos z | set zp to trigRef.getPos z | ||
if ( ( xp | if ( ( xp + yp + zp ) == 0 ) ; arrow is not in gameworld | ||
message "Arrow has hit an actor!" | message "Arrow has hit an actor!" | ||
set triggered to 0 | set triggered to 0 | ||
Line 244: | Line 244: | ||
set ay to trigRef.getAngle y | set ay to trigRef.getAngle y | ||
set az to trigRef.getAngle z | set az to trigRef.getAngle z | ||
elseif ( xp == ox</pre> | elseif ( xp == ox && yp == oy && zp == oz ) ; arrow stopped moving | ||
if ( triggered == 1 ) ; arrow never bounced | |||
message "Arrow stuck in a surface!" | |||
else ; arrow bounced | |||
message "Arrow is lying on the ground!" | |||
endif | |||
set triggered to 0 | |||
else | |||
set ox to xp | |||
set oy to yp ; set 'previous frame' coordinates to check against next frame | |||
set oz to zp | |||
endif | |||
endif | |||
end</pre> | |||
Admittedly, that script isn't terribly useful in itself, but it should give you a starting point in setting up your own scripts on arrows. | |||
==Determining who fired the arrow== | |||
Remember that issue I said we'd tackle later? A problem may arise if an NPC fires an arrow at the player - if it collides with our trigger zone, the script has no way of recognizing that it wasn't fired by the player. One way to fix that would be to track the arrow's flight over the course of a few frames to determine where it is heading. But there may not be enough time for that, especially in low frame-rate situations. | |||
The [[GetAngle]] function provides a more useful method. At the moment the player fires an arrow, the arrow's angles will match the player's precisely. So a bit of code like this: | |||
<pre>begin onTriggerMob | |||
set trigRef to getActionRef | |||
if ( trigRef.isAmmo && trigRef.getAngle z == player.getAngle z && trigRef.getAngle x == player.getAngle x ) | |||
message "The player fired the arrow." | |||
endif | |||
end</pre> | |||
...would likely do what we want. However, the player might move the mouse before the trigger zone detects the arrow, which would cause the angles to differ. So to be safe, you'd want to compare the arrow's angles to the player's within a reasonable margin of error. | |||
==Tying it all together== | |||
Bear in mind that if two scripts attempt to use this approach at the same time, they will conflict with each other. Therefore, it's a good idea to conditionalize things as specifically as possible. For instance, if you only need to detect a certain type of arrow, then you would enable the trigZone only while the player has that type of arrow equipped. | |||
Also, you'll probably realize that the usefulness of trigger zones isn't limited to detecting arrows. For instance, by keeping a trigZone glued in front of the player at all times, you could detect what item or actor is in the crosshairs at any given moment, and perform actions on the target. I hope to find time to post some more tutorials on the uses of trigger zones, but in the meantime, have fun experimenting. | |||
[[Category:Useful_Code]] |