如何在Unity3D中用手指触摸移动
How to touch to move with finger in Unity3D
我想为移动设备移动我的游戏对象,就像玩家可以触摸屏幕上的任何地方并移动 his/her 手指,游戏对象将随之移动,而不是移动触摸位置。
这是我到目前为止的脚本
void Update () {
if (Input.touchCount > 0)
{
Touch _touch = Input.GetTouch(0); // screen has been touched, store the touch
if( _touch.phase == TouchPhase.Moved) // finger moved
{
//offset = Camera.main.ScreenToWorldPoint(new Vector3(_touch.position.x, _touch.position.y, theplayer.transform.position.z)) - theplayer.transform.position;
touchPos = Camera.main.ScreenToWorldPoint(new Vector3(_touch.position.x, _touch.position.y, theplayer.transform.position.z));
theplayer.transform.position = Vector2.Lerp(theplayer.transform.position, touchPos, Time.deltaTime*5f);
}
else if(_touch.phase == TouchPhase.Ended){
touchPos = Vector3.zero;
offset = Vector3.zero;
}
}
} // end
脚本几乎可以正常工作,但问题是当我在屏幕上触摸时,游戏对象在手指下方移动,所以我看不到游戏对象。我不想要这个我想要触摸屏幕上的任何地方并用手指移动而不是移动到手指位置。
谢谢。
我已经自己解决了这里的问题代码。
// Update is called once per frame
void Update () {
if (Input.touchCount > 0)
{
_touch = Input.GetTouch(0); // screen has been touched, store the touch
if(_touch.phase == TouchPhase.Began){
isDragging = true;
offset = Camera.main.ScreenToWorldPoint(new Vector2(_touch.position.x, _touch.position.y)) - theplayer.transform.position;
}
else if(_touch.phase == TouchPhase.Ended){
offset = Vector2.zero;
isDragging = false;
}
}
if(isDragging){
Vector2 _dir = Camera.main.ScreenToWorldPoint(new Vector2(_touch.position.x, _touch.position.y));
_dir = _dir - offset;
theplayer.transform.position = Vector2.Lerp(theplayer.transform.position, _dir, Time.deltaTime * speed);
}
} // end
我想为移动设备移动我的游戏对象,就像玩家可以触摸屏幕上的任何地方并移动 his/her 手指,游戏对象将随之移动,而不是移动触摸位置。
这是我到目前为止的脚本
void Update () {
if (Input.touchCount > 0)
{
Touch _touch = Input.GetTouch(0); // screen has been touched, store the touch
if( _touch.phase == TouchPhase.Moved) // finger moved
{
//offset = Camera.main.ScreenToWorldPoint(new Vector3(_touch.position.x, _touch.position.y, theplayer.transform.position.z)) - theplayer.transform.position;
touchPos = Camera.main.ScreenToWorldPoint(new Vector3(_touch.position.x, _touch.position.y, theplayer.transform.position.z));
theplayer.transform.position = Vector2.Lerp(theplayer.transform.position, touchPos, Time.deltaTime*5f);
}
else if(_touch.phase == TouchPhase.Ended){
touchPos = Vector3.zero;
offset = Vector3.zero;
}
}
} // end
脚本几乎可以正常工作,但问题是当我在屏幕上触摸时,游戏对象在手指下方移动,所以我看不到游戏对象。我不想要这个我想要触摸屏幕上的任何地方并用手指移动而不是移动到手指位置。
谢谢。
我已经自己解决了这里的问题代码。
// Update is called once per frame
void Update () {
if (Input.touchCount > 0)
{
_touch = Input.GetTouch(0); // screen has been touched, store the touch
if(_touch.phase == TouchPhase.Began){
isDragging = true;
offset = Camera.main.ScreenToWorldPoint(new Vector2(_touch.position.x, _touch.position.y)) - theplayer.transform.position;
}
else if(_touch.phase == TouchPhase.Ended){
offset = Vector2.zero;
isDragging = false;
}
}
if(isDragging){
Vector2 _dir = Camera.main.ScreenToWorldPoint(new Vector2(_touch.position.x, _touch.position.y));
_dir = _dir - offset;
theplayer.transform.position = Vector2.Lerp(theplayer.transform.position, _dir, Time.deltaTime * speed);
}
} // end