用Touch旋转GameObject

时间:2015-02-12 00:50:45

标签: unity3d rotation

我在下面有这个代码并且在键盘中工作,但是不能用于触摸。

Quaternion rot = transform.rotation;  float z = rot.eulerAngles.z;      
z-= Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;   
rot =    Quaternion.Euler( 0, 0, z );   
transform.rotation = rot;

我需要上面的代码才能触摸,怎么做?

2 个答案:

答案 0 :(得分:2)

也许您应该阅读Api以获取Unity Scripting http://docs.unity3d.com/ScriptReference/Input.html

的输入

您可以使用getTouch方法旋转对象 这里是文档

http://docs.unity3d.com/ScriptReference/Input.GetTouch.html

您可以使用delta进行拖动,或使用Position旋转对象

所以这就好像不确定它的工作原理应该适用于拖动

 if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
     Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
     z-=touchDeltaPosition.x * rotSpeed * Time.deltaTime;   
     rot =    Quaternion.Euler( 0, 0, z );   
  }

答案 1 :(得分:0)

它应该与上面的几乎相同,但是代替x轴是水平的,我们使用y轴作为垂直的,这个代码也是拖动

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
     Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
Vector3 pos = transform.position; 
Vector3 velocity = new Vector3(0, touchDeltaPoition.y * maxSpeed * Time.deltaTime, 0);
pos += rot * velocity;
}