DroneSimulator/Assets/Scripts/Speedometer.cs

35 lines
803 B
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 TMPro;
using UnityEngine;
public class Speedometer : MonoBehaviour
{
[SerializeField] private TMP_Text _text;
[SerializeField] private Rigidbody _drone;
private Vector3 _previousPosition;
public float SpeedMetersPerSecond { get; private set; }
public float SpeedKilometersPerHour { get; private set; }
void Start()
{
// Ñîõðàíèòü íà÷àëüíóþ ïîçèöèþ è âðåìÿ
_previousPosition = _drone.position;
}
private void FixedUpdate()
{
float distance = Vector3.Distance(_previousPosition, _drone.position);
_previousPosition = _drone.position;
float speed = distance / Time.deltaTime;
speed *= 3.6f;
_text.text = $"{(int)speed} ÊÌ/×";
}
}