Unity hero battle logic

First, lock the enemy through the hero combat script.
When locking the enemy, move it through the movement script.
When the attack range is reached, the attack logic is triggered.
The player's value is subtracted from the enemy's
health. When the enemy has no health, the enemy is destroyed.
The enemy
Insert image description here
player's
Insert image description here
attack animation is started
Insert image description here
. The attack animation is turned off.
Insert image description here

hero battle

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeroCombat : MonoBehaviour
{
    
       // 英雄战斗方式
    public enum HeroAttackType {
    
     Melee,RanGed};
    public HeroAttackType heroAttackType;

    // 锁定敌人 攻击范围
    public GameObject targetedEnemy;
    public float attackRange;
    //public float rotateSpeedForAttack;
    // 移动脚本 统计脚本 动画组件
    private Movement moveScript;
    private Stats statsScript;
    private Animator anim;

    // 英雄生死 攻击触发 
    //public bool basicAtkIdle = false;
    public bool isHeroAlive;
    public bool perfoemMeleeAttack = true;
    // Start is called before the first frame update
    void Start()
    {
    
       // 实例化移动脚本
        moveScript = GetComponent<Movement>();
        statsScript = GetComponent<Stats>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
    
    // 当有敌人的时候
        if(targetedEnemy != null)
        {
    
    //敌人和玩家的距离超过攻击范围
            if (Vector3.Distance(gameObject.transform.position,targetedEnemy.transform.position) > attackRange)
            {
    
    // 向敌人方向前进达到攻击范围停止
                moveScript.agent.SetDestination(targetedEnemy.transform.position);
                moveScript.agent.stoppingDistance = attackRange;

                Quaternion rotationToLookAt = Quaternion.LookRotation(targetedEnemy.transform.position - transform.position);

            }
            else
            {
    
    // 当确定了攻击类型攻击敌人
                if (heroAttackType == HeroAttackType.Melee)
                {
    
    // 开始攻击
                    if(perfoemMeleeAttack)
                    {
    
    // 发出攻击消息
                        Debug.Log("Attack The Minion");

                        // 开始攻击的协同方法
                        StartCoroutine(MeleeAttackInterval());
                    }
                }
            }
        }
        IEnumerator MeleeAttackInterval()
        {
    
    // 暂时不能触发攻击
            perfoemMeleeAttack = false;
            // 攻击动画启动
            anim.SetBool("Basic Attack", true);
            // 攻击时间控制攻速
            yield return new WaitForSeconds(statsScript.attackTime);
            // 当敌人没有在锁定的时候
            if(targetedEnemy == null)
            {
    
    // 关闭攻击动画
                anim.SetBool("Basic Attack", false);
                //perfoemMeleeAttack = true;
                
            }
            MeleeAttack(); // 扣血方法

        }
        
    }

    public void MeleeAttack()
    {
    
    // 当锁定到敌人的时候
        if(targetedEnemy != null)
        {
    
       // 判断锁定的是不是敌人
            if(targetedEnemy.GetComponent<Targetable>().enemyType == Targetable.EnemyType.Minion)
            {
    
    // 敌人的血量减去玩家的攻击
                targetedEnemy.GetComponent<Stats>().health -= statsScript.attackDmg;
            }
        }
        perfoemMeleeAttack = true; // 可以再次触发攻击
    }
    
}

hero status

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // 血条显示需要

public class Health : MonoBehaviour
{
    
       // 玩家血条变量 血条数值变量
    public Slider playerSlider3D;
    private Slider playerSlider2D;
    private Stats statsScript;

    void Start()
    {
    
       // 获取屏幕上的血条 玩家和屏幕上的血条都等于血条数值
        statsScript = GameObject.FindGameObjectWithTag("player").GetComponent<Stats>();
        playerSlider2D = GetComponent<Slider>();
        playerSlider3D.maxValue = statsScript.maxHealth;
        playerSlider2D.maxValue = statsScript.maxHealth;
        statsScript.health = statsScript.maxHealth;
    }


    void Update()
    {
    
       // 屏幕血条数值通过血条数值改动 玩家数值和屏幕数值同步
        playerSlider2D.value = statsScript.health;
        playerSlider3D.value = playerSlider2D.value;
    }
}

enemy status

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // 血条显示需要

public class EnemyHealth : MonoBehaviour
{
    
       // 敌人血条变量 血条数值变量
    public Slider enemySlider3D;
    
    private Stats statsScript;

    void Start()
    {
    
       // 获取敌人的血条 赋值最大血量 血量等于最大血量
        statsScript = GameObject.FindGameObjectWithTag("Enemy").GetComponent<Stats>();
        
        enemySlider3D.maxValue = statsScript.maxHealth;

        statsScript.health = statsScript.maxHealth;
    }


    void Update()
    {
    
       // 血量实时同步

        enemySlider3D.value = statsScript.health;
    }
}

Unify data

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Stats : MonoBehaviour
{
    
    // 最大血量 当前血量 攻击数值 攻击间隔
    public float maxHealth;
    public float health;
    public float attackDmg;
    //public float attackSpeed;
    public float attackTime;

    HeroCombat heroCombatScript; // 英雄战斗脚本
    
    void Start()
    {
    
       // 获得英雄战斗脚本
        heroCombatScript = GameObject.Find("Player").GetComponent<HeroCombat>();
    }



    void Update()
    {
    
    // 当血量等于0
        if(health <= 0)
        {
    
    // 销毁对象 敌人为空 攻击停止
            Destroy(gameObject);
            heroCombatScript.targetedEnemy = null;
            heroCombatScript.perfoemMeleeAttack = false;
        }
    }
}

move logic

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Movement : MonoBehaviour
{
    
       // 导航组件 旋转速度移动 旋转速度 英雄战斗组件
    public NavMeshAgent agent;

    public float rotateSpeedMovement = 0.1f;
    public float rotateVelocity;

    private HeroCombat heroCombatScript;
    // Start is called before the first frame update
    void Start()
    {
    
       // 实例化导航 英雄战斗组件
        agent = gameObject.GetComponent<NavMeshAgent>();
        // 
        heroCombatScript = GetComponent<HeroCombat>();
    }

    // Update is called once per frame
    void Update()
    {
    
    // 当玩家英雄战斗中有战斗的敌人的时候
        if (heroCombatScript.targetedEnemy != null)
        {
    
    // 这个玩家有英雄战斗组件的时候
            if(heroCombatScript.targetedEnemy.GetComponent<HeroCombat>() != null)
            {
    
    // 当条件满足的时候
                if (heroCombatScript.targetedEnemy.GetComponent<HeroCombat>().isHeroAlive)
                {
    
    // 英雄战斗中没有敌人
                    heroCombatScript.targetedEnemy = null;
                }
            }
            
        }
        if (Input.GetMouseButtonDown(1))
        {
    
    // 开放一个射线变量
            RaycastHit hit; // 屏幕上的射线
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit,Mathf.Infinity))
            {
    
    // 射线碰到地面
                if(hit.collider.tag == "Floor")
                {
    
    // 玩家移动到目的地 敌人为空时 
                    agent.SetDestination(hit.point);
                    heroCombatScript.targetedEnemy = null;
                }
                
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_60839745/article/details/128748506