Зависает Unity 3d
Добавлено: 09 янв 2020, 11:11
Здравствуйте. Помогите пожалуйста с решением проблемы. При соприкосновении Enemy и Player зависает Unity. До этого момента всё шло хорошо. Код написан по уроку на ютубе. Если нужна ссылка на видео, скину.
Вот код на Enemy
[RequireComponent (typeof (NavMeshAgent))]
public class Enemy : LivingEntity {
public enum State {Idle, Chasing, Attacking};
State currentState;
NavMeshAgent pathfinder;
Transform target;
Material skinMaterial;
Color originalColor;
float attackDistanceThreshold = 0.5f;
float timeBetweenAttack = 1;
float nextAttackTime;
float myCollisionRadius;
float targetCollisionRadius;
protected override void Start () {
base.Start ();
pathfinder = GetComponent <NavMeshAgent> ();
skinMaterial = GetComponent <Renderer>().material;
originalColor = skinMaterial.color;
currentState = State.Chasing;
target = GameObject.FindGameObjectWithTag ("Player").transform;
myCollisionRadius = GetComponent<CapsuleCollider>().radius;
targetCollisionRadius = target.GetComponent<CapsuleCollider>().radius;
StartCoroutine (UpdatePath ());
}
void Update () {
if (Time.time > nextAttackTime) {
float sqrDstToTarget = (target.position - transform.position).sqrMagnitude;
if (sqrDstToTarget < Mathf.Pow (attackDistanceThreshold + myCollisionRadius + targetCollisionRadius, 2)) {
nextAttackTime = Time.time + timeBetweenAttack;
StartCoroutine(Attack());
}
}
}
IEnumerator Attack() {
currentState = State.Attacking;
pathfinder.enabled = false;
Vector3 originalPosition = transform.position;
Vector3 dirToTarget = (target.position - transform.position).normalized;
Vector3 attackPosition = target.position - dirToTarget*(myCollisionRadius);
float attackSpeed = 3;
float percent = 0;
skinMaterial.color = Color.red;
while (percent <= 1) {
percent += Time.deltaTime * attackSpeed;
float interpolation = (-Mathf.Pow(percent,2) + percent) * 4;
transform.position = Vector3.Lerp(originalPosition, attackPosition, interpolation);
yield return null;
}
skinMaterial.color = originalColor;
currentState = State.Chasing;
pathfinder.enabled = true;
}
IEnumerator UpdatePath() {
float refreshRate = 0.25f;
while (target != null) {
if (currentState == State.Chasing){
Vector3 dirToTarget = (target.position - transform.position).normalized;
Vector3 targetPosition = target.position - dirToTarget*(myCollisionRadius + targetCollisionRadius + attackDistanceThreshold/2);
if (!dead){
pathfinder.SetDestination (targetPosition);
}
yield return new WaitForSeconds(refreshRate);
}
}
}
}
Вот код на Enemy
[RequireComponent (typeof (NavMeshAgent))]
public class Enemy : LivingEntity {
public enum State {Idle, Chasing, Attacking};
State currentState;
NavMeshAgent pathfinder;
Transform target;
Material skinMaterial;
Color originalColor;
float attackDistanceThreshold = 0.5f;
float timeBetweenAttack = 1;
float nextAttackTime;
float myCollisionRadius;
float targetCollisionRadius;
protected override void Start () {
base.Start ();
pathfinder = GetComponent <NavMeshAgent> ();
skinMaterial = GetComponent <Renderer>().material;
originalColor = skinMaterial.color;
currentState = State.Chasing;
target = GameObject.FindGameObjectWithTag ("Player").transform;
myCollisionRadius = GetComponent<CapsuleCollider>().radius;
targetCollisionRadius = target.GetComponent<CapsuleCollider>().radius;
StartCoroutine (UpdatePath ());
}
void Update () {
if (Time.time > nextAttackTime) {
float sqrDstToTarget = (target.position - transform.position).sqrMagnitude;
if (sqrDstToTarget < Mathf.Pow (attackDistanceThreshold + myCollisionRadius + targetCollisionRadius, 2)) {
nextAttackTime = Time.time + timeBetweenAttack;
StartCoroutine(Attack());
}
}
}
IEnumerator Attack() {
currentState = State.Attacking;
pathfinder.enabled = false;
Vector3 originalPosition = transform.position;
Vector3 dirToTarget = (target.position - transform.position).normalized;
Vector3 attackPosition = target.position - dirToTarget*(myCollisionRadius);
float attackSpeed = 3;
float percent = 0;
skinMaterial.color = Color.red;
while (percent <= 1) {
percent += Time.deltaTime * attackSpeed;
float interpolation = (-Mathf.Pow(percent,2) + percent) * 4;
transform.position = Vector3.Lerp(originalPosition, attackPosition, interpolation);
yield return null;
}
skinMaterial.color = originalColor;
currentState = State.Chasing;
pathfinder.enabled = true;
}
IEnumerator UpdatePath() {
float refreshRate = 0.25f;
while (target != null) {
if (currentState == State.Chasing){
Vector3 dirToTarget = (target.position - transform.position).normalized;
Vector3 targetPosition = target.position - dirToTarget*(myCollisionRadius + targetCollisionRadius + attackDistanceThreshold/2);
if (!dead){
pathfinder.SetDestination (targetPosition);
}
yield return new WaitForSeconds(refreshRate);
}
}
}
}