Unity_lab3/Unity3_lab/Assets/Scripts/TriggerPlatform.cs

63 lines
2.3 KiB
C#

using UnityEngine;
using System.Collections.Generic;
public class TriggerPlatform : MonoBehaviour
{
[Header("Настройки Шаров")]
public List<GameObject> hazardBalls;
[SerializeField] private float initialPushForce = 5f; // Начальный толчок для старта движения
[SerializeField] private float triggerDelay = 0.5f; // Задержка перед началом движения
private bool hasBeenTriggered = false;
void Start()
{
// При старте убеждаемся, что все шары статичны
foreach (GameObject ball in hazardBalls)
{
Rigidbody rb = ball.GetComponent<Rigidbody>();
if (rb != null)
{
rb.isKinematic = true;
}
if (ball.GetComponent<RollingHazard>() == null)
{
ball.AddComponent<RollingHazard>();
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.GetComponent<BallController>() != null && !hasBeenTriggered)
{
hasBeenTriggered = true;
Invoke("ActivateHazards", triggerDelay);
}
}
private void ActivateHazards()
{
foreach (GameObject ball in hazardBalls)
{
Rigidbody rb = ball.GetComponent<Rigidbody>();
if (rb != null)
{
// 1. включаем физику
rb.isKinematic = false;
// 2. Определяем направление к игроку
Vector3 direction = (transform.position - ball.transform.position).normalized;
Vector3 pushDirection = (ball.transform.position - transform.position);
pushDirection.y = 0;
pushDirection = pushDirection.normalized;
rb.AddForce(transform.forward * initialPushForce, ForceMode.Impulse);
// Активируем скрипт RollingHazard для постоянного преследования игрока
ball.GetComponent<RollingHazard>().StartPursuit(FindObjectOfType<BallController>().transform);
}
}
}
}