我想创建一个标记为"paddle“的2d游戏对象,当我先触摸它的时候,它会跟随我的手指在屏幕上移动,但什么都不会发生,它只是静止不动。
void Update()
{
// detect if there was a touch
if (Input.touchCount > 0)
{
switch (touch.phase)
{
//When a touch begins:
case TouchPhase.Began:
//get the position of the touch
touch = Input.GetTouch(0);
touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
//detect if the touch is on the paddle at the beginning of the touch (if YES then we can move the paddle)
touchHit = Physics2D.Raycast(touchPosition, Camera.main.transform.forward);
if (touchHit.collider.tag == "paddle")
{
paddleTouched = true;
}
break;
//When the touch is moving, if it has hit the paddle at the beginning the paddle will move
case TouchPhase.Moved:
if (paddleTouched == true)
{
gameObject.transform.position = touchPosition;
}
break;
//When a touch has ended everything ends :3
case TouchPhase.Ended:
paddleTouched = false;
break;
}
}
}
发布于 2019-03-25 02:49:12
我应该把这些
touch = Input.GetTouch(0);
touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
在switch case语句之前。
https://stackoverflow.com/questions/55326875
复制相似问题