所以我有一个用户可以通过触摸旋转的对象。如果需要,这是脚本:
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°时我想做什么?有什么办法吗?
答案 0 :(得分:0)
您可以沿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
}
答案 1 :(得分:0)
totalRotated += (yDifference + xDifference);
(我创建了一个名为totalRotated
的游泳池)并且它的效果非常好。