@GoodPony

Как посчитать количество пройденных кругов?

Здравствуйте! У меня есть рычаг который крутится вокруг своей оси. Нужно посчитать сколько кругов он прошёл. Вот моя попытка.
if (m_iCompletedCircle == 0 & angle < 0) {
			tempFirstCicle=true;
			var rot = Quaternion.AngleAxis(0, Lever.right);
			Lever.transform.localRotation = rot * Lever.transform.localRotation;
			Debug.Log("Заблокировано");
		} else if (m_fDelta != 0) {
			var rot = Quaternion.AngleAxis(angle, Lever.right);
			Lever.transform.localRotation = rot * Lever.transform.localRotation;

			if (tempFirstCicle) {
				m_iCompletedCircle= m_iCompletedCircle+1;
				tempFirstCicle=false;
			}

		}
		
		if (m_iCompletedCircle >= 1 & angle > 0) {
			var currentLeverPosittion = Lever.transform.localRotation;
			if (currentLeverPosittion ==saveBeginLeverRotation) {
				m_iCompletedCircle= m_iCompletedCircle+1;
				Debug.Log("Пройдено кругов: "+m_iCompletedCircle);

			} 
			else if (m_iCompletedCircle >= 1 & angle < 0) {
				if (currentLeverPosittion == saveBeginLeverRotation) {
					m_iCompletedCircle= m_iCompletedCircle-1;
					Debug.Log("Пройдено кругов: "+m_iCompletedCircle);
				}
			}
  • Вопрос задан
  • 45 просмотров
Пригласить эксперта
Ответы на вопрос 1
K0TlK
@K0TlK
Буллю людей.
Не знаю что ты там намудрил, твой код абсолютно нечитабельный.
Прибавляешь к вращению, чтобы крутилось в одну сторону, вычитаешь, чтобы крутилось в другую. Полный круг - 360 градусов. Вращение делишь на 360 = количество полных кругов. Математика 6 класс привет.

Кодстайл, который используется в дотнет.

public class Lever : MonoBehaviour
{
    private float _rotation = 0f;

    private int LapsCount => (int)(_rotation / 360);

    private void Rotate(float angle)
    {
        _rotation += angle;
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, _rotation));
    }

    private void Update()
    {
        if (Input.GetKey(KeyCode.A))
        {
            Rotate(100f * Time.deltaTime);
        }

        if (Input.GetKey(KeyCode.D))
        {
            Rotate(-100f * Time.deltaTime);
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log(LapsCount);
        }
    }
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы