88 lines
2.3 KiB
C#
88 lines
2.3 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using TMPro;
|
||
|
||
public class ObjectPhysicsInfo : MonoBehaviour
|
||
{
|
||
|
||
public TextMeshProUGUI infoText;
|
||
private Rigidbody _rb;
|
||
|
||
private float _startTime;
|
||
private float _startHeight;
|
||
private bool _isFalling = false;
|
||
private bool _hasLanded = false;
|
||
private float _startMass;
|
||
|
||
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 = _startMass;
|
||
float gravityForce = mass * 9.8f;
|
||
|
||
float theoreticalTime = Mathf.Sqrt((2 * dropHeight) / 9.8f);
|
||
|
||
if (infoText != null)
|
||
{
|
||
infoText.text =
|
||
$"Висота: {dropHeight:F2} м\n" +
|
||
$"Час падіння: {fallTime:F2} с\n" +
|
||
$"Швидкість: {impactSpeed:F2} м/с\n" +
|
||
$"Маса: {mass:F2} кг\n" +
|
||
$"Сила тяжіння: {mass:F2} × 9.8 = ?? Н\n" +
|
||
$"Прискорення: 9.8 м/с²";
|
||
}
|
||
|
||
AirTrailController trail = GetComponentInChildren<AirTrailController>();
|
||
if (trail != null)
|
||
{
|
||
trail.Deactivate();
|
||
}
|
||
|
||
Invoke("ClearText", 15f);
|
||
}
|
||
}
|
||
|
||
void ClearText()
|
||
{
|
||
if (infoText != null)
|
||
infoText.text = "";
|
||
}
|
||
}
|