public static bool isShow = false;//特效
public int enemyHp = 3;//敌人血量
public int playerHp = 10;//玩家血量
public static GameManager gm;//unity单例写法
void Start () {
gm = this;
}
if (Input.GetMouseButtonDown(0))
{
//Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//Ray ray = new Ray(playerPos.position, zhiZhu.position);//射线:玩家到蜘蛛的位置,源头只有蜘蛛
RaycastHit hit;
//if (Physics.Raycast(ray, out hit,100))
Vector3 dir = transform.TransformDirection(transform.forward);//TransformDirection向前方向
if (Physics.Raycast(playerPos.position,dir,out hit,100))
//playerPos.position:枪口 dir:向前 out hit:射线 100:长度
{
Debug.DrawLine(playerPos.position, hit.point, Color.red);//绘制一条红色的射线
//射击动画
//an.Play(“Shoot”);
//an.CrossFade(“Shoot”, 0.167f);
ps = PlayerState.Shoot;
GetState(ps);
//播放射击音效
audios.Play();
if (zhiZhu != null)
{
//调用蜘蛛的播放和特效和声音的方法
zhiZhu.transform.GetComponent<Enemy>().FindEnemy(hit.collider.gameObject);//射线本身
}
//if (hit.collider.gameObject.name.Equals(“Enemy”))
//{
// print(“aaaaaaaaaaaa”);
// Destroy(hit.collider.gameObject, 1f);
//}
}
}
float timer = 0.167f;//计时器
void FindPlayer()
{
if (zhiZhu!=null)
{
//玩家距离蜘蛛10米
if (Vector3.Distance(transform.position, zhiZhu.position) <= 10)//自身的位置距离敌人的位置
{
//玩家距离蜘蛛5米,自动攻击敌人
if (Vector3.Distance(transform.position, zhiZhu.position) <= 5)//自身的位置距离敌人的位置
{
ps = PlayerState.Shoot;
GetState(ps);
timer -= Time.deltaTime;
if (timer < 0)
{
//播放射击音效
audios.Play();
timer = 0.167f;
}
}
else//5–10
{
//an.Play(“Run”);
ps = PlayerState.Run;
GetState(ps);
transform.Translate(transform.forward * Time.deltaTime * speed);
}
}
else
{
ps = PlayerState.Idle;
GetState(ps);
}
}
}
//蜘蛛距离玩家的距离10米
if (Vector3.Distance(transform.position, player.position) <= 10)
{ //蜘蛛距离玩家的距离5米
if (Vector3.Distance(transform.position, player.position) <= 5)
{
es = EnemyState.Attack;
GetState(es);
}
else
{
es = EnemyState.Walk;
GetState(es);
//transform.Translate(-transform.forward * Time.deltaTime * speed);
Vector3 dir = transform.position – player.position;//朝向玩家
transform.Translate(dir * Time.deltaTime * speed);
}
}
else
{
es = EnemyState.Idle;
GetState(es);
}
if (GameManager.isShow)
{
//克隆爆炸特效并伴随音效
Instantiate(blood, transform.position, Quaternion.identity);
AudioSource.PlayClipAtPoint(clip, transform.position);
}
}
//定义一个接收射线点击的对象,然后判断是否是蜘蛛
public void FindEnemy(GameObject g)//创建一个游戏对象
{
if (g.transform.CompareTag(“Enemy”))//通过标签查找敌人
{
StartCoroutine(Wait());//调用协程
}
}
IEnumerator Wait()
{
print(1111);
yield return new WaitForSeconds(0.3f);//0.3秒后执行
GameManager.gm.enemyHp–;//通过调用GameManager实例化
if (GameManager.gm.enemyHp==0)
{
//克隆爆炸特效并伴随爆炸音效
Instantiate(blood, transform.position, Quaternion.identity);
audios.Play();
Destroy(gameObject,0.3f);
}
}
float h = Input.GetAxis(“Horizontal”);
float v = Input.GetAxis(“Vertical”);
Vector3 pos = Vector3.zero;
//按下键盘使玩家走动/.
if (h != 0 || v != 0)
{
//转方向
if (h < 0)//左边
{
transform.rotation = Quaternion.Euler(0, -90, 0);
pos = transform.forward * v + transform.right * -h;//控制前后左右
}
if (h > 0)//右边
{
transform.rotation = Quaternion.Euler(0, 90, 0);
pos = transform.forward * v + transform.right * -h;
}
if (v < 0)//后面
{
transform.rotation = Quaternion.Euler(0, 180, 0);
pos = transform.forward * v + transform.right * h;
}
if (v > 0)//前面
{
transform.rotation = Quaternion.Euler(0, 0, 0);
pos = transform.forward * v + transform.right * h;
}
//an.Play(“Walk”);
//先切换到Walk状态,然后再调用Walk动画
ps = PlayerState.Walk;
GetState(ps);
transform.Translate(pos * Time.deltaTime * speed);
