Files
ScienceLab.GravityForce/Assets/Materials/Scripts/MoonObjectInfo.cs
2026-03-17 13:40:09 +02:00

89 lines
2.4 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 System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class MoonObjectInfo : MonoBehaviour
{
public TextMeshProUGUI infoText;
private Rigidbody _rb;
private float _startTime;
private float _startHeight;
private bool _isFalling = false;
private bool _hasLanded = false;
private float _startMass;
private const float moonGravity = 1.62f;
void Start()
{
_rb = GetComponent<Rigidbody>();
_startMass = _rb.mass;
if (infoText != null)
infoText.text = "";
}
void Update()
{
if (infoText != null)
{
infoText.transform.parent.LookAt(Camera.main.transform);
infoText.transform.parent.Rotate(0, 180, 0);
}
}
public void OnRelease()
{
_startTime = Time.time;
_startHeight = transform.position.y;
_isFalling = true;
_hasLanded = false;
if (infoText != null)
infoText.text = "";
}
void OnCollisionEnter(Collision collision)
{
if (_isFalling && !_hasLanded && collision.gameObject.CompareTag("Ground"))
{
_hasLanded = true;
_isFalling = false;
float fallTime = Time.time - _startTime;
float dropHeight = _startHeight - transform.position.y;
float impactSpeed = _rb.velocity.magnitude;
float mass = _rb.mass;
float gravityForce = mass * moonGravity;
float theoreticalTime = Mathf.Sqrt((2 * dropHeight) / moonGravity);
if (infoText != null)
{
infoText.text =
$"Висота: {dropHeight:F2} м\n" +
$"Час падіння: {fallTime:F2} с\n" +
$"Швидкість удару: {impactSpeed:F2} м/с\n" +
$"Маса: {mass:F2} кг\n" +
$"Сила тяжіння: {mass:F2} × {moonGravity} = ?? Н\n" +
$"Прискорення: {moonGravity} м/с² (Місяць)";
}
AirTrailController trail = GetComponentInChildren<AirTrailController>();
if (trail != null)
{
trail.Deactivate();
}
Invoke("ClearText", 15f);
}
}
void ClearText()
{
if (infoText != null)
infoText.text = "";
}
}