给玩家选择攻击形态
放入子弹和生成地点
在子弹的身上放一个脚本
英雄战斗脚本
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;
// 远距离攻击 是否能攻击 子弹类型 子弹生成点
[Header("Ranged Varialdes")]
public bool performRangedAttack = true;
public GameObject projeprefab;
public Transform projSpawnPoint;
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;
}
else
{
// 当确定了攻击类型攻击敌人
if (heroAttackType == HeroAttackType.Melee)
{
Quaternion rotationToLookAt = Quaternion.LookRotation(targetedEnemy.transform.position - transform.position);
moveScript.agent.SetDestination(transform.position);
// 开始攻击
if (perfoemMeleeAttack)
{
// 发出攻击消息
Debug.Log("Attack The Minion");
// 开始攻击的协同方法
StartCoroutine(MeleeAttackInterval());
}
}
if (heroAttackType == HeroAttackType.RanGed)
{
Quaternion rotationToLookAt = Quaternion.LookRotation(targetedEnemy.transform.position - transform.position);
moveScript.agent.SetDestination(transform.position);
// 开始攻击
if (performRangedAttack)
{
// 发出攻击消息
Debug.Log("Attack The Minion");
// 开始攻击的协同方法
StartCoroutine(RangedAttackInterval());
}
}
}
}
else if (targetedEnemy == null)
{
// 关闭攻击动画
anim.SetBool("Basic Attack", false);
perfoemMeleeAttack = true;
}
}
IEnumerator MeleeAttackInterval()
{
// 暂时不能触发攻击
perfoemMeleeAttack = false;
// 攻击动画启动
anim.SetBool("Basic Attack", true);
// 攻击时间控制攻速
yield return new WaitForSeconds(statsScript.attackTime);
// 当敌人没有在锁定的时候
// 扣血方法
MeleeAttack();
}
IEnumerator RangedAttackInterval()
{
// 暂时不能触发攻击
performRangedAttack = false;
// 攻击动画启动
anim.SetBool("Basic Attack", true);
// 攻击时间控制攻速
yield return new WaitForSeconds(statsScript.attackTime);
// 当敌人没有在锁定的时候
// 扣血方法
RangedAttack();
}
public void MeleeAttack()
{
// 当锁定到敌人的时候
if(targetedEnemy != null)
{
// 判断锁定的是不是敌人
if(targetedEnemy.GetComponent<Targetable>().enemyType == Targetable.EnemyType.Minion)
{
// 敌人的血量减去玩家的攻击
targetedEnemy.GetComponent<Stats>().health -= statsScript.attackDmg;
}
}
perfoemMeleeAttack = true; // 可以再次触发攻击
}
public void RangedAttack()
{
// 当锁定到敌人的时候
if (targetedEnemy != null)
{
// 判断锁定的是不是敌人
if (targetedEnemy.GetComponent<Targetable>().enemyType == Targetable.EnemyType.Minion)
{
// 敌人的血量减去玩家的攻击
SpawnRangedProj("Minion", targetedEnemy);
}
}
performRangedAttack = true; // 可以再次触发攻击
}
void SpawnRangedProj(string typeOfEnemy, GameObject targetedEnemyObj)
{
float dmg = statsScript.attackDmg;
Instantiate(projeprefab, projSpawnPoint.transform.position, Quaternion.identity);
if (typeOfEnemy == "Minion")
{
projeprefab.GetComponent<RangedProjectile>().targetType = typeOfEnemy;
projeprefab.GetComponent<RangedProjectile>().target = targetedEnemyObj;
}
}
}
子弹脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RangedProjectile : MonoBehaviour
{
// 攻击数值 攻击目标
public float damage;
public GameObject target;
// 敌人类型 子弹攻击速度
public string targetType;
public float velocity = 5;
void Update()
{
if (target != null)
{
// 子弹有目标的时候子弹朝目标位置靠近
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, velocity * Time.deltaTime);
}
if (target == null)
{
// 子弹没有目标的时候摧毁子弹
Destroy(gameObject);
}
else if (Vector3.Distance(transform.position, target.transform.position) < 0.5f)
{
// 如果子弹和敌人距离小于0.5f
if (targetType == "Minion")
{
// 判断是不是爪牙如果是就扣血并且损毁子弹
target.GetComponent<Stats>().health -= damage;
Destroy(gameObject);
}
}
}
}