【问题标题】:Android Touch Movement安卓触控动作
【发布时间】:2014-10-04 10:04:18
【问题描述】:
我想知道如何通过触摸将对象向左或向右移动。比如:
public float speed;
void FixedUpdate ()
{
float LeftRight = Input.GetAxis ("Horizontal");
Vector3 Movement = new Vector3 ( LeftRight, 0, 0);
rigidbody.AddForce(Movement * speed);
}
但只是为了触摸屏幕。屏幕的前半部分为左侧,另一部分为右侧。
【问题讨论】:
标签:
c#
unity3d
2d
unity3d-2dtools
【解决方案1】:
对于android或ios中的触摸输入类型,请使用Input.GetTouch。
这个想法是获取触摸的位置,然后通过使用Screen.width 获取屏幕宽度来决定它是触摸屏幕的左侧还是右侧。
public float speed;
void FixedUpdate ()
{
float LeftRight = 0;
if(Input.touchCount > 0){
// touch x position is bigger than half of the screen, moving right
if(Input.GetTouch(0).position.x > Screen.width / 2)
LeftRight = 1;
// touch x position is smaller than half of the screen, moving left
else if(Input.GetTouch(0).position.x < Screen.width / 2)
LeftRight = -1;
}
Vector3 Movement = new Vector3 ( LeftRight, 0, 0);
rigidbody.AddForce(Movement * speed);
}