Святослав Ясновидец
Мастер
(1978)
1 день назад
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boss_Run : StateMachineBehaviour
{
public float speed = 2.5f;
public float attackRange = 3f;
Transform player;
Transform bossTransform;
Boss boss;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
player = GameObject.FindGameObjectWithTag("Player").transform;
bossTransform = animator.GetComponent<Transform>();
boss = animator.GetComponent<Boss>();
}
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
boss.LookAtPlayer();
// Вычисляем направление движения
Vector2 target = new Vector2(player.position.x, bossTransform.position.y);
Vector2 direction = (target - (Vector2)bossTransform.position).normalized;
// Перемещаем босса в направлении игрока
bossTransform.Translate(direction * speed * Time.deltaTime);
// Проверяем расстояние до игрока для атаки
if (Vector2.Distance(player.position, bossTransform.position) <= attackRange)
{
animator.SetTrigger("Attack");
}
}
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
animator.ResetTrigger("Attack");
}
}
using System.Collections.Generic;
using UnityEngine;
public class Boss_Run : StateMachineBehaviour
{
public float speed = 2.5f;
public float attackRange = 3f;
Transform player;
Rigidbody2D rb;
Boss boss;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
player = GameObject.FindGameObjectWithTag("Player").transform;
rb = animator.GetComponent<Rigidbody2D>();
boss = animator.GetComponent<Boss>();
}
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
boss.LookAtPlayer();
Vector2 target = new Vector2(player.position.x, rb.position.y);
Vector2 newPos = Vector2.MoveTowards(rb.position, target, speed * Time.fixedDeltaTime);
rb.MovePosition(newPos);
if (Vector2.Distance(player.position, rb.position) <= attackRange)
{
animator.SetTrigger("Attack");
}
}
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
animator.ResetTrigger("Attack");
}
}
Чтоб не Rigidbody2D передвигался а сам персонаж