首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我有一个代码,必须使我能够移动一个2d游戏对象与手指触摸,但它不工作,我如何解决它?

我有一个代码,必须使我能够移动一个2d游戏对象与手指触摸,但它不工作,我如何解决它?
EN

Stack Overflow用户
提问于 2019-03-25 02:06:31
回答 1查看 22关注 0票数 0

我想创建一个标记为"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;
        }
    }

}
EN

回答 1

Stack Overflow用户

发布于 2019-03-25 02:49:12

我应该把这些

touch = Input.GetTouch(0);
touchPosition = Camera.main.ScreenToWorldPoint(touch.position);

在switch case语句之前。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55326875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档