O'Reilly logo

Mastering Unity 2D Game Development - Second Edition by Simon Jackson, Ashley Godbold

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Selecting an attack

The player will select an attack and then select the enemy to perform the attack. To allow the player to select various attacks from the HUD, we will create a new script called Attack in the Assets/Scripts folder, as follows:

using UnityEngine; 
using System.Collections; 
 
public class Attack : MonoBehaviour { 
 
    public bool attackSelected=false; 
    public int hitAmount=0; 
 
    public void Smack(){ 
        hitAmount=5; 
        AttackTheEnemy(); 
    } 
 
    public void Wack(){ 
        hitAmount=10; 
        AttackTheEnemy(); 
    } 
 
    public void Kick(){ 
        hitAmount=15; 
        AttackTheEnemy(); 
    } 
 
    public void Chop(){ 
        hitAmount=20; 
        AttackTheEnemy(); 
    } 
 
    public void AttackTheEnemy(){ 
        attackSelected=true; 
    } 
}  

Essentially, when each button is hit, it will reference a different attack with a different hit ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required