Rpg combat system tutorial for Unity with simple state machine – part 4

Share

Rpg combat system tutorial for Unity with simple state machine – part 4

In the previous Rpg combat system tutorial for Unity with simple state machine – part 3 we gave our player character a script to manage its combat states and to deal damage to the enemy, but to make it effective we need to give to the enemy a revised javascript enemyAi.js script to implement the functions called by the CombatSystem.js script by our player character. Edit the enemyAi script in the demon gameobject, it should look like the following :

/* enemyAi.js - Unity rpg combat system tutorial - www.Gameobject.net */
/* Author : Piffa of www.Gameobject.net */

#pragma strict
/* delcaring states to pass to player gameobject */
public var isAttacking : boolean = false ; 
public var enemyHealthPoints : int = 10 ; // Health pool for the enemy
public var enemyDamage : int = 1 ; // Damage dealt by enemy per hit

private var  playAnim : boolean = true ;

function Update () {
    if (playAnim){	//check if attack animation was played
        WaitSeconds(); //wait random seconds for animation
       }
//Destroy enemy if hp drops to zero and destroy it if so
	if(enemyHealthPoints < 0){
		Destroy(gameObject);
		Debug.Log("Enemy is Dead !!!");
		}
}

function WaitSeconds(){
	playAnim = false; 
    var randomWait = Random.Range(0, 6);              
    print ("Wait" + randomWait + " seconds"); //debug          
    yield WaitForSeconds(randomWait); //Wait a random amount of time  
    isAttacking = true ;  //set isAttacking state to true               
    animation.Play("punch"); //play attack animation
    yield WaitForSeconds(animation["punch"].length) ;//wait for animation duration
    isAttacking = false ;//set isAttacking state to false
    playAnim = true ; // detect when anim is complete
}

//Function to call when hit by player
function TakeDamage(dmg : int){
	enemyHealthPoints -= dmg ;
	Debug.Log("Enemy suffered " + dmg  +" damages");
}

This rpg combat system tutorial for unity is almost ended, you can press play and test if the enemy and player health points are scaled correctly when they are in close combat and their Box triggers components intersect. This Rpg combat system for Unity with simple state machine is to be improved in the future, or by yourself, with two additional fixes. As of now it doesn’t matter if the attacker is facing the defender, so every gameobject that intersects with an attacker is hit and damaged regardless of the weapon orientation. Another quick fix its to make the player die if he runs out of health points and create a quick game over sequence!

12-Unity-rpg-hack-slash-combat-system-tutorial


I hope you enjoyed this Rpg combat system tutorial for Unity with simple state machine and that it gave you some ideas to make your combat system working !

You can find all the scripts used in this Unity rpg combat system tutorial with state machine in this page :

UNITY RPG COMBAT SYSTEM TUTORIAL WITH STATE MACHINE JAVASCRIPT FULL CODE DOWNLOAD EXAMPLES

 

 

Unity rpg combat system tutorial with state machine – part 1

Unity rpg combat system tutorial with state machine – part 2

Unity rpg combat system tutorial with state machine – part 3

Page 1 of 11

About Piffa

Aspiring game programmer , game designer , online player , pen and paper rpg player , web designer and rocket scientist , is slowly trying to achieve at least one aspiration. Piffa has little skill scattered between various programming languages as C , C++ , javascript , fortran and good handling of HTML and he's trying to build a game by his own that will crash the game industry. Or maybe not. You know , game industry is weird.