-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerAndMotorSpeed.cs
50 lines (43 loc) · 2 KB
/
TimerAndMotorSpeed.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using UnityEngine;
using UnityEngine.UI;
public class TimerAndMotorSpeed : MonoBehaviour
{
public Text timerText; // Посилання та текстове поле для відображення часу
public Text timer2Text; // Посилання на текстове поле для відображення періода
public GameObject circle; // Посилання на об'єкт "circle" для встановлення motor speed
private float startTime; // Час початку відліку
private HingeJoint2D hingeJoint; // Посилання на компонент HingeJoint2D
private void Start()
{
startTime = Time.time; // Запам'ятаємо час для початку відліку
hingeJoint = circle.GetComponent<HingeJoint2D>(); // Отримуємо компонент HingeJoint2D
if (hingeJoint != null)
{
// Генеруємо випадкове значення motor speed
float randomMotorSpeed = Random.Range(100f, 200f);
// Встановлюємо motor speed для компонента HingeJoint2D
JointMotor2D motor = hingeJoint.motor;
motor.motorSpeed = randomMotorSpeed;
hingeJoint.motor = motor;
}
}
private void Update()
{
float currentTime = Time.time - startTime;
timerText.text = currentTime.ToString("F2");
}
//обчислення періода обертання
public void CalculateRotationPeriod()
{
if (hingeJoint != null)
{
float motorSpeed = hingeJoint.motor.motorSpeed;
if (motorSpeed != 0)
{
float rotationPeriod = 360f / Mathf.Abs(motorSpeed);
float rotationFrequency=1/rotationPeriod;
timer2Text.text = "Період: " + rotationPeriod.ToString("F2") + "\nЧастота: " + rotationFrequency.ToString("F2");
}
}
}
}