Vuforia开发AR卡牌对战游戏(二):实现
接着上一篇,我们再继续实现我们的卡牌对战游戏,上一次忘记了将血条的样式改为水平的Filed样式。
这里我们需要几个Tag:KnightAI,RemoteAI,KnightWeapen,RemoteBullet,分别给到近战,远程,近战的武器,远程的子弹上。
由于我们有两个角色,一个远程一个近战,所以我们要写两个脚本。
首先写近战的脚本:KnightCtrl,先说一下大致思路:近战角色有血量,要获得自身血条的引用,自己在被子弹击中时会减血,还需要判断敌人是否死亡,敌人死亡需要嘲讽,自己是否死亡;需要每帧去获得远程敌人的引用(因为不知道啥时候会识别到敌人识别图,所以不能在Start中获得),到了一定的距离可以向着敌人移动,到了攻击范围就去攻击敌人。下面是主要代码和注释:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class KnightCtrl : MonoBehaviour
{
public float m_Life = 100f; //血量
public float sightRange = 20f; //视野范围,到了视野范围就向着对方移动
public float attackRange = 2f; //攻击范围
public Image bloodImage; //血条
public static float weaponDamage = 20f; //自己的武器伤害值
private Animator m_Animator;//动画机
private bool enemyHasDie; //敌人是否死亡
// Use this for initialization
void Start ()
{
m_Animator = this.GetComponent<Animator>();
//待机
m_Animator.SetBool("Run", false);
m_Animator.SetBool("NextAttack", false);
enemyHasDie = false;
}
// Update is called once per frame
void Update ()
{
GameObject remoteEnemy = GameObject.FindGameObjectWithTag("RemoteAI");
if (remoteEnemy != null && !enemyHasDie && m_Life > 0f) //识别图加载出来后而且敌人没有死而且自己还活着
{
float dis = Vector3.Distance(this.transform.position, remoteEnemy.transform.p