using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyController1: MonoBehaviour
{
// 判断塔目标是否存在,判断两者距离,如果距离在范围内,切换到走路状态,走向塔;当距离在可攻击范围内,停止走路,开始攻击
float walkdistance = 4.2f;
float attackdistance = 0.8f;
float speed = 0.005f;
public Slider enemySlider;
public float enemyLife = 100;
private Animator animator;
private AudioSource enemyvoice;
void Start()
{
animator = GetComponent<Animator>();
enemyvoice = GetComponent<AudioSource>();
}
void Update()
{
GameObject tower = GameObject.Find("Tower");
if (tower != null)
{
float distance = Vector3.Distance(tower.transform.position, transform.position);
Debug.Log(distance);
if (distance < walkdistance)
{
animator.SetBool("walk", true);
transform.position = Vector3.MoveTowards(transform.position, tower.transform.position, speed);
if (distance <= attackdistance)
{
speed = 0;
animator.SetBool("walk", false);
animator.SetBool("attack", true);
}
}
//人物停止攻击回到待机状态
if (tower.GetComponent<TowerController>().towerLife <= 0)//判断塔的血量是否小于0
{
animator.SetBool("attack", false);
animator.SetBool("walk", false);
animator.SetBool("idle", true);
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Ball(Clone)")
{
animator.SetTrigger("hit");
enemyvoice.Play();
enemyLife -= 5f;
enemySlider.value = enemyLife;
if (enemyLife <= 0)
{
animator.SetBool("die", true);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TowerController : MonoBehaviour
{
// 1.当被攻击的时候,掉血2.发出警报的声音3.可以从炮口发出子弹还击,4.塔的血量到零,塔身变暗,敌人停止攻击
public float towerLife = 100f;
public Slider towerSlider;
private AudioSource towervoice;
float shotDistance = 2.2f;
public GameObject bullet;
public Transform shotPosition;
public float speed = 4;
float timego = 1;
void Start()
{
towervoice = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
//当敌人走到一定距离的时候,开始发射炮弹
GameObject enemy = GameObject.Find("Enemy");
if (enemy != null)
{
float distance = Vector3.Distance(enemy.transform.position, transform.position);
if (distance <= shotDistance)
{
//发射
if (towerLife > 0 && enemy.GetComponent<EnemyController1>().enemyLife > 0)
{
timego += Time.deltaTime;
if (timego >= 1)
{
GameObject shot = Instantiate(bullet, shotPosition.position, shotPosition.rotation);
Rigidbody rb = shot.GetComponent<Rigidbody>();
rb.velocity = shotPosition.forward * speed;
timego = 0;
}
}
}
}
}
private void OnTriggerEnter(Collider other)//当碰撞体被碰到,并且是斧头的时候,表示攻击,掉血
{
if (other.gameObject.name == "Sword")
{
if (!towervoice.isPlaying)
{
towervoice.Play();
}
towerLife -= 5f;
towerSlider.value = towerLife;
if (towerLife <= 0)
{
towervoice.Stop();
Renderer[] renderers = GetComponentsInChildren<Renderer>();
foreach (Renderer ren in renderers)
{
ren.material.color = Color.black;
}
//攻击停止
}
}
}
这个一连摄像头就报这个错误,但是不连接摄像头处理就可以正常运行。
换一个摄像头,或者换一个软件虚拟摄像头呢?
检查一下那个Enemy对象上有没有挂载脚本EnemyController,没有就会报这个错。
gameobject.find和getcompoenet这种操作不要放在update里,很耗费性能,我建议你先把update里的这些逻辑摘出来,然后再看一下,因为根据你的描述来看并不像你的代码逻辑的问题