【问题标题】:Unity 2D Using Drag To Move Up And DownUnity 2D 使用拖动上下移动
【发布时间】:2015-04-05 20:19:00
【问题描述】:

我想将我的游戏改造成 Android 游戏,就像乒乓球一样。我希望能够在手机上拖动拨片。提前感谢您的帮助。这是我最旧的代码:

  using UnityEngine;
using System.Collections;

public class MoveRacket : MonoBehaviour {
        public float speed = 30;
        public string axis = "Vertical";

        void FixedUpdate () {
        float v = Input.GetAxisRaw (axis);
          GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * speed;



    }
}

这是我的旧代码,但它仍然无法正常工作。

   using UnityEngine;
using System.Collections;

public class MoveRacket : MonoBehaviour {
    public float speed = 30;
    public string axis = "Vertical";
    public object racket = "Racket";
    public bool touchInput = true;
    public Vector2 touchPos;





    void FixedUpdate () {

        //used to not have anything in parentheses
        float v = Input.GetAxisRaw (axis);
        //GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * speed;
        if (Input.touchCount == 1)
        {
            Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
            Vector2 touchPos = new Vector2(wp.x, wp.y);
            if (racket == Physics2D.OverlapPoint(touchPos));
            {
                GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * speed;

            }
        }
    }
}

这是我当前已修复的代码。

using UnityEngine;
using System.Collections;

public class MoveRacket : MonoBehaviour {
    public float speed = 30;
    public string axis = "Vertical";
    public object racket = "Racket";
    public bool touchInput = true;
    public Vector2 touchPos;





    void FixedUpdate () {

        //used to not have anything in parentheses
        //float v = Input.GetAxisRaw (axis);
        float v = Input.GetAxisRaw (axis);
        GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, v) * speed;
        if (Input.touchCount == 1)
        {
            Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
            Vector2 touchPos = new Vector2(wp.x, wp.y);
            if (Racket.Collider2D == Physics2D.OverlapPoint(touchPos));
            {
                this.transform.position.y = wp.y

            }
        }
    }
}

回答:上面的代码是固定的,应该可以使用。

【问题讨论】:

    标签: c# android unity3d touch 2d


    【解决方案1】:

    你说你想拖动,因此你需要触摸 首先,您需要检查用户是否正在触摸屏幕,然后使用 raycast2d 来检查他是否正在触摸桨,并使用相同的逻辑将其保持在您用于鼠标的手指位置。

    首先自己尝试一下,用这个作为提示 http://answers.unity3d.com/questions/577314/how-to-detect-if-a-sprite-was-object-was-touched-i.html

    谢谢

    【讨论】:

    • 感谢您的支持,一旦我有我的电脑,我会尝试一下。
    • 我现在可以使用您的帮助。我自己试过了,但考虑到我只是在移动 y,我不确定与我有什么关系,而且如果你有时间我想帮助修复我的错误,我也会遇到错误。提前谢谢
    • 好的,不要使用刚体组件,只需在第二个 if 语句中使用它: this.transform.position = new vector3(wp.x,wp.y,0);现在确定你是否不想改变 y 位置然后使用 this.tranform.position.y 而不是 wp.y 告诉我错误并告诉我触摸是否有效?
    • 重新编辑并说明了上面的当前错误,感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多