mirror of
https://github.com/BotChain-Robots/ui.git
synced 2026-07-08 15:07:22 +02:00
switch from Unity version control to git
This commit is contained in:
82
Assets/DCMotorModule.cs
Normal file
82
Assets/DCMotorModule.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class DCMotorModule : ModuleBase
|
||||
{
|
||||
private static DCMotorControlPanel motorControlPanel;
|
||||
public Transform motorShaft;
|
||||
|
||||
public float rotationSpeed = 90f;
|
||||
private float targetPosition = 0f;
|
||||
private Coroutine followCoroutine;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
if (motorControlPanel == null)
|
||||
{
|
||||
motorControlPanel = FindObjectOfType<DCMotorControlPanel>(true);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnSelect()
|
||||
{
|
||||
if (followCoroutine != null)
|
||||
StopCoroutine(followCoroutine);
|
||||
|
||||
followCoroutine = StartCoroutine(FollowPositionForFrames());
|
||||
motorControlPanel.Initialize(this);
|
||||
}
|
||||
|
||||
private IEnumerator FollowPositionForFrames()
|
||||
{
|
||||
RectTransform panelRect = motorControlPanel.GetComponent<RectTransform>();
|
||||
|
||||
float duration = 0.5f;
|
||||
float timer = 0f;
|
||||
|
||||
while (timer < duration)
|
||||
{
|
||||
Vector3 worldPos = transform.position;
|
||||
Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos) + new Vector3(150f, 0f, 0f);
|
||||
panelRect.position = screenPos;
|
||||
|
||||
timer += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
public override void DeSelect()
|
||||
{
|
||||
motorControlPanel.HidePanel();
|
||||
}
|
||||
|
||||
public void Rotate(float degrees, int direction)
|
||||
{
|
||||
float rotation = degrees * direction;
|
||||
targetPosition += rotation;
|
||||
this.SendToControlLibrary("DC", targetPosition);
|
||||
StartCoroutine(RotateOverTime(rotation));
|
||||
}
|
||||
|
||||
private IEnumerator RotateOverTime(float rotation)
|
||||
{
|
||||
float currentRotationDegrees = 0f;
|
||||
|
||||
while (Mathf.Abs(currentRotationDegrees) < Mathf.Abs(rotation))
|
||||
{
|
||||
float step = rotationSpeed * Time.deltaTime;
|
||||
if (Mathf.Abs(currentRotationDegrees + step) > Mathf.Abs(rotation))
|
||||
{
|
||||
step = Mathf.Abs(rotation) - Mathf.Abs(currentRotationDegrees);
|
||||
}
|
||||
|
||||
motorShaft.Rotate(Vector3.up, step * Mathf.Sign(rotation));
|
||||
currentRotationDegrees += step * Mathf.Sign(rotation);
|
||||
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user