簡體   English   中英

如何知道一個游戲對象在Unity中旋轉了多少?

[英]How to know how much a gameobject has rotated in Unity?

因此,我有一個用戶可以觸摸旋轉的對象。 如果需要,下面是它的腳本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpinWithTaps : MonoBehaviour {

float lastX;
public float xDifference;
public float xDecreaseSpeed;
int xDirection = 1;

float lastY;
public float yDifference;
public float yDecreaseSpeed;
int yDirection = 1;

void Update()
{
    //turn in y Axis
    if (Input.GetMouseButtonDown(0)) xDifference = 0;
    else if (Input.GetMouseButton(0))
    {
        xDifference = Mathf.Abs((lastX - Input.GetAxis("Mouse X")) * 1.8f);

        if (lastX < Input.GetAxis("Mouse X"))
        {
            xDirection = -1;
            transform.Rotate(Vector3.up, -xDifference, relativeTo: Space.World);
        }

        if (lastX > Input.GetAxis("Mouse X"))
        {
            xDirection = 1;
            transform.Rotate(Vector3.up, xDifference, relativeTo: Space.World);
        }

        lastX = -Input.GetAxis("Mouse X");
    }
    else
    {
        if (xDifference > 0)
        {
            if (xDifference > 20) xDecreaseSpeed = 0.3f;
            else if (xDifference > 15) xDecreaseSpeed = 0.23f;
            else if (xDifference > 10) xDecreaseSpeed = 0.16f;
            else if (xDifference > 5) xDecreaseSpeed = 0.09f;
            else xDecreaseSpeed = 0.02f;

            xDifference -= xDecreaseSpeed;

            if (xDifference < 0) xDifference = 0;
        }
        if (xDifference < 0)
        {
            if (xDifference < 20) xDecreaseSpeed = 0.3f;
            else if (xDifference < 15) xDecreaseSpeed = 0.23f;
            else if (xDifference < 10) xDecreaseSpeed = 0.16f;
            else if (xDifference < 5) xDecreaseSpeed = 0.09f;
            else xDecreaseSpeed = 0.02f;

            xDifference += xDecreaseSpeed;

            if (xDifference > 0) xDifference = 0;
        }
        transform.Rotate(Vector3.up, xDifference * xDirection, relativeTo: Space.World);
    }

    //turn in x Axis
    if (Input.GetMouseButtonDown(0)) yDifference = 0;
    else if (Input.GetMouseButton(0))
    {
        yDifference = Mathf.Abs((lastY - Input.GetAxis("Mouse Y")) * 1.8f);

        if (lastY < Input.GetAxis("Mouse Y"))
        {
            yDirection = 1;
            transform.Rotate(Vector3.right, yDifference, relativeTo: Space.World);
        }

        if (lastY > Input.GetAxis("Mouse Y"))
        {
            yDirection = -1;
            transform.Rotate(Vector3.right, -yDifference, relativeTo: Space.World);
        }

        lastY = -Input.GetAxis("Mouse Y");
    }
    else
    {
        if (yDifference > 0)
        {
            if (yDifference > 20) yDecreaseSpeed = 0.3f;
            else if (yDifference > 15) yDecreaseSpeed = 0.23f;
            else if (yDifference > 10) yDecreaseSpeed = 0.16f;
            else if (yDifference > 5) yDecreaseSpeed = 0.09f;
            else yDecreaseSpeed = 0.02f;

            yDifference -= yDecreaseSpeed;

            if (yDifference < 0) yDifference = 0;
        }
        if (yDifference < 0)
        {
            if (yDifference < 20) yDecreaseSpeed = 0.3f;
            else if (yDifference < 15) yDecreaseSpeed = 0.23f;
            else if (yDifference < 10) yDecreaseSpeed = 0.16f;
            else if (yDifference < 5) yDecreaseSpeed = 0.09f;
            else yDecreaseSpeed = 0.02f;

            yDifference += yDecreaseSpeed;

            if (yDifference > 0) yDifference = 0;
        }
        transform.Rotate(Vector3.right, yDifference * yDirection, relativeTo: Space.World);
    }
}

我想在gameObject旋轉時做一些事情,比如說……總共90度。 像這樣:

if (totalRotated >= 90)
{
    //do something
}

如何找到totalRotated 謝謝。

編輯:或者如果我想做一個總旋轉480°的事情怎么辦? 有什么辦法嗎?

您可以沿x,y或z軸旋轉。

if (gameObject.transform.rotation.x == 90) // You might want to use >= to check if it is greater than 90 in this case
{
    // do something
}

哈哈,我實際上找到了一個相對簡單的解決方案。 我將以下代碼行添加到我的Update函數中:

totalRotated += (yDifference + xDifference);

(並且我創建了一個名為totalRotated的浮點數),並且效果非常好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM