DevLog3: Basic enemy AI and Enemy interaction


For this update I decided to implement a basic left-to right patrolling behaviour on the enemy. For this reason I included a sensor child object to the enemy parent that tells it when a collision has occurred by using an OnTriggerEnter2D function. This function uses the parent LocalScale attribute of the transform to switch the  x axis scale to a negative, which results in the sprite flipping. Once sprite has flipped, a boolean function was set to detect minuscule scale values, using the Epsilon field from the  Mathf class. If this change has occurred, checking via an if statement, I could induce controlled motion into the parent enemy by modifying the RigidBody2D component by multiplying the value of a floating point speed variable into the normalised vector. To introduce a simple player detection-and-attack behaviour I calculated the separation vector from the player to the enemy and scripted frame-timed instantiation of a projectile seed whose origin was the point 0,0 of the enemy; a Vector3.Up rotation, and lastly the separation vector as direction, that will result in the projectile homing to the player. To the instantiated projectile the velocity was given by the product between a float variable and the transform.up vector.

 In order to detect the position of the player so the local scale of enemy results correct or it faces the player if it approaches from left or right, I used if conditionals that checked for the absolute values of  x axis scale. Once the condition is met the product by one or minus one on the scale would result in a mirroring effect on the enemy sprite. As additional features to facing the correct orientation of the player, I equaled the Vector2 magnitudes of the velocity property of the RidigBody2D class to zero once it detected the enemy within a distance. This gives the sensation that enemy is stopping to face the player once is detected and animation of enemy attacking will play.  If the player retreats or hasn't entered the detection distance, the attack animation would stop, and the patrolling movement resumes.

Files

RunceCaster AI and interaction.zip Play in browser
Sep 22, 2023
RC Level Blocking v1.zip 21 MB
Sep 14, 2023

Get Codename: Rune Caster

Leave a comment

Log in with itch.io to leave a comment.