Unity_lab3/Unity3_lab/Assets/Scripts/TrampolineScript.cs

32 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
public class TrampolineScript : MonoBehaviour
{
// ⚙️ Сила, с которой шарик будет отталкиваться.
[Tooltip("Сила отскока. Настройте это значение в Инспекторе для каждого трамплина.")]
[SerializeField] private float bounceForce = 15f;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
Rigidbody playerRigidbody = collision.gameObject.GetComponent<Rigidbody>();
if (playerRigidbody == null)
{
Debug.LogError("Игрок с тегом 'Player' должен иметь компонент Rigidbody для отскока!");
return;
}
Vector3 normal = collision.contacts[0].normal;
playerRigidbody.linearVelocity = Vector3.zero;
playerRigidbody.AddForce(normal * bounceForce, ForceMode.Impulse);
Debug.Log($"Игрок отскочил от трамплина по нормали: {normal.normalized}");
}
}
}