繁体   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