58 lines
2.0 KiB
C#
58 lines
2.0 KiB
C#
using UnityEngine;
|
||
|
||
public class RollingHazard : MonoBehaviour
|
||
{
|
||
[Header("Настройки Преследования")]
|
||
[SerializeField] private float pursuitForce = 50f; // Сила, с которой шар катится за игроком
|
||
[SerializeField] private float maxSpeed = 10f; // Ограничение максимальной скорости
|
||
[SerializeField] private float destroyYLevel = -5f; // Координата Y, при которой шар удаляется
|
||
|
||
private Transform playerTarget;
|
||
private Rigidbody rb;
|
||
private bool isPursuing = false;
|
||
|
||
void Awake()
|
||
{
|
||
rb = GetComponent<Rigidbody>();
|
||
|
||
if (GetComponent<Collider>() != null)
|
||
{
|
||
GetComponent<Collider>().isTrigger = false;
|
||
}
|
||
|
||
}
|
||
|
||
public void StartPursuit(Transform target)
|
||
{
|
||
playerTarget = target;
|
||
isPursuing = true;
|
||
}
|
||
|
||
void FixedUpdate()
|
||
{
|
||
// 1. Логика преследования
|
||
if (isPursuing && playerTarget != null)
|
||
{
|
||
// Направление к игроку
|
||
Vector3 directionToPlayer = (playerTarget.position - transform.position).normalized;
|
||
directionToPlayer.y = 0; // Только горизонтальное движение
|
||
|
||
// Добавляем силу, чтобы шар катился
|
||
rb.AddForce(directionToPlayer * pursuitForce, ForceMode.Acceleration);
|
||
|
||
// Ограничиваем горизонтальную скорость
|
||
Vector3 flatVel = new Vector3(rb.linearVelocity.x, 0, rb.linearVelocity.z);
|
||
if (flatVel.magnitude > maxSpeed)
|
||
{
|
||
Vector3 limitedVel = flatVel.normalized * maxSpeed;
|
||
rb.linearVelocity = new Vector3(limitedVel.x, rb.linearVelocity.y, limitedVel.z);
|
||
}
|
||
}
|
||
|
||
// 2. Логика удаления при падении
|
||
if (transform.position.y < destroyYLevel)
|
||
{
|
||
Destroy(gameObject);
|
||
}
|
||
}
|
||
} |