我在编辑器中正在使用一个脚本来旋转精灵,我想翻译此脚本以使用“触摸输入”而不是鼠标。
我使用以下脚本根据鼠标拖动来旋转游戏中的精灵。我真的没有触摸输入的经验,所以我尝试过的东西是我在其他一些使用触摸输入的代码中看到的。 基本上,我更改了if语句: 来自
if (Input.GetMouseButtonDown(0))
{
deltaRotation = 0f;
previousRotation = angleBetweenPoints(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
收件人:
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
deltaRotation = 0f;
previousRotation = angleBetweenPoints(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
}
这是我现在的代码,我正在用它来用鼠标旋转精灵:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
deltaRotation = 0f;
previousRotation = angleBetweenPoints(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
else if (Input.GetMouseButton(0))
{
currentRotation = angleBetweenPoints(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
deltaRotation = Mathf.DeltaAngle(currentRotation, previousRotation);
if (Mathf.Abs(deltaRotation) > deltaLimit)
{
deltaRotation = deltaLimit * Mathf.Sign(deltaRotation);
}
previousRotation = currentRotation;
transform.Rotate(Vector3.back * Time.deltaTime, deltaRotation);
}
else
{
transform.Rotate(Vector3.back * Time.deltaTime, deltaRotation);
deltaRotation = Mathf.Lerp(deltaRotation, 0, deltaReduce * Time.deltaTime);
}
}
答案 0 :(得分:0)
您的if/else
语句使用的等效条件不仅仅是TouchPhase.Moved
。
看起来像以下的TouchPhases
一样。
TouchPhase.Began
代替Input.GetMouseButtonDown(0)
TouchPhase.Moved
代替Input.GetMouseButton(0)
TouchPhase.Ended
代替最后的else
PS:在UnityAnswers上给出相同的答案
所以,它看起来可能像这样:
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
deltaRotation = 0f;
previousRotation = angleBetweenPoints(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
else if (touch.phase == TouchPhase.Moved)
{
currentRotation = angleBetweenPoints(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
deltaRotation = Mathf.DeltaAngle(currentRotation, previousRotation);
if (Mathf.Abs(deltaRotation) > deltaLimit)
{
deltaRotation = deltaLimit * Mathf.Sign(deltaRotation);
}
previousRotation = currentRotation;
transform.Rotate(Vector3.back * Time.deltaTime, deltaRotation);
}
else if (touch.phase == TouchPhase.Ended)
{
transform.Rotate(Vector3.back * Time.deltaTime, deltaRotation);
deltaRotation = Mathf.Lerp(deltaRotation, 0, deltaReduce * Time.deltaTime);
}
}
}