Catalogue

Monday, July 11, 2016

Scripting 3: AI: Fansher_AI

Fansher_AI is the script used to control Fanshers.
This is an overview of the Fansher_AI Script.
VOID Start()  Meant to be used only once to mark initial variables.
guardlock: records the initial position. Used for guardian fanshers who only patrol the area around their spawn point.
body: The rigidbody2D attached to the fansher, there isn't one by default.
truemaxspeed: Records the initial maxspeed.
shotref: Provides a link to the Shooter_AI script.
anim: Links to the object's animator component.
zpos: The z position, used to decide the enemy's depth.
sizer: The enemy's scale. Is set to the enemy's initial x scale
VOID Update() The meat of the script. 
  • Basic info is first retrieved. Para and hpRef are set to read the para, enemyHP, hurt, and canBeControlled are set based on the counterpart variables within the Enemy_Health script.
  • Acceleration is set to (1+Mathf.Abs(hspeed)*baseaccel)/3. So the fansher's speed increases at a slower rate the faster it moves.
After this, the four wants(direction) variables are read, and movement is decided based on their values. Fspeed (vertical) always adds double acceleration. Hspeed (horizontal) adds x1, or x3 if the Fansher is flying towards the opposite direction at the time.
Fspeed and Hspeed are stopped separately through lerping to 0 by 0.95f.
Final movements are set through:

transform.Translate(Vector2.right * truemaxspeed/2 * 0.01f * 100 * Time.deltaTime);
transform.Translate(Vector3.up * fspeed * truemaxspeed/2 * Time.deltaTime);
  • A circlecast is used to check the distance between the enemy and player. If the player exits the circle's radius, the enemy object becomes inactive.
  • Another circlecast is used to check if the player is within the Fansher's line of sight. 
  • A raycast is used to ensure the Fansher avoids being too close to water.
  • If the player is within the Fansher's line of site, the Fansher will call ProjectChoose() randomly based on Random.Range(0, bulletchance).
  • Finally, the debugging variable "wantsToMove" is checked, and the AI variables are set according to the debug commands.
IEnumerator ProjectChoose() The meat of the script. 
  • The shot prepping animation is set to let the player know the fansher is about to fire. 
  • hspeed and fspeed are both multiplied by 0.5f to slow down the fansher.
  • body.velocity is set to vector2.zero.
  • Waits for a customizable amount of time "bulletTossWait." 
  • Activates shotRef.ProjectChoose(); on the Shooter_AI script.

No comments:

Post a Comment