【问题标题】:unity multitouch android 2Dunity 多点触控 android 2D
【发布时间】:2014-03-23 14:30:35
【问题描述】:

我正在创建一个游戏,主角可以跳跃、左右移动。我有一个问题。多点触控不起作用。我正在使用 C#,我在互联网上到处搜索,但没有找到答案。

我的代码:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof (Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class TouchControls : MonoBehaviour {

    // GUI textures
    public GUITexture guiLeft;
    public GUITexture guiRight;
    public GUITexture guiJump;

    // Movement variables
    public float moveSpeed = 5f;
    public float jumpForce = 50f;
    public float maxJumpVelocity = 2f;

    // Movement flags
    private bool moveLeft, moveRight, doJump = false;

    // Update is called once per frame
    void Update () {

        // Check to see if the screen is being touched
        if (Input.touchCount > 0)
        {
            // Get the touch info
            Touch t = Input.GetTouch(0);

            // Did the touch action just begin?
            if (t.phase == TouchPhase.Began)
            {
                // Are we touching the left arrow?
                if (guiLeft.HitTest(t.position, Camera.main))
                {
                    Debug.Log("Touching Left Control");
                    moveLeft = true;
                }

                // Are we touching the right arrow?
                if (guiRight.HitTest(t.position, Camera.main))
                {
                    Debug.Log("Touching Right Control");
                    moveRight = true;
                }

                // Are we touching the jump button?
                if (guiJump.HitTest(t.position, Camera.main))
                {
                    Debug.Log("Touching Jump Control");
                    doJump = true;
                }
            }

            // Did the touch end?
            if (t.phase == TouchPhase.Ended)
            {
                // Stop all movement
                doJump = moveLeft = moveRight = false;
                rigidbody2D.velocity = Vector2.zero;
            }
        }

        // Is the left mouse button down?
        if (Input.GetMouseButtonDown(0))
        {
            // Are we clicking the left arrow?
            if (guiLeft.HitTest(Input.mousePosition, Camera.main))
            {
                Debug.Log("Touching Left Control");
                moveLeft = true;
            }

            // Are we clicking the right arrow?
            if (guiRight.HitTest(Input.mousePosition, Camera.main))
            {
                Debug.Log("Touching Right Control");
                moveRight = true;
            }

            // Are we clicking the jump button?
            if (guiJump.HitTest(Input.mousePosition, Camera.main))
            {
                Debug.Log("Touching Jump Control");
                doJump = true;
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            // Stop all movement on left mouse button up
            doJump = moveLeft = moveRight = false;
            rigidbody2D.velocity = Vector2.zero;
        }
    }

    void FixedUpdate()
    {
        // Set velocity based on our movement flags.
        if (moveLeft)
        {
            rigidbody2D.velocity = -Vector2.right * moveSpeed;
        }

        if (moveRight)
        {
            rigidbody2D.velocity = Vector2.right * moveSpeed;
        }

        if (doJump)
        {
            // If we have not reached the maximum jump velocity, keep applying force.
            if (rigidbody2D.velocity.y < maxJumpVelocity)
            {
                rigidbody2D.AddForce(Vector2.up * jumpForce);
            } else {
                // Otherwise stop jumping
                doJump = false;
            }
        }
    }
}

【问题讨论】:

    标签: c# android unity3d 2d multi-touch


    【解决方案1】:

    你必须列出所有的触摸:

    if (Input.touchCount > 0) {
        for(int i = 0; i < Input.touchCount; i++) {
            Touch t = Input.GetTouch(0);
            ..
            ..
    }
    

    这意味着,每次触摸都将单独处理。 官方文档here.

    祝你的游戏好运。

    【讨论】:

    • 它不起作用,但谢谢你的回答......请提供更多帮助
    • 由于只有 JavaScript 示例文档,可能会出现语法错误。
    • 你能帮帮我吗?
    【解决方案2】:

    用这个替换你的脚本:

    using UnityEngine;
    using System.Collections;
    
    [RequireComponent(typeof (Rigidbody2D))]
    [RequireComponent(typeof(BoxCollider2D))]
    public class TouchControls : MonoBehaviour {
    
    public GUITexture guiLeft;
    public GUITexture guiRight;
    public GUITexture guiJump;
    
    public float moveSpeed = 5f;
    public float jumpForce = 50f;
    public float maxJumpVelocity = 2f;
    
    private bool moveLeft, moveRight, doJump = false;
    
    void Update () {
    
        if (Input.touchCount > 0) {
            for(int i = 0; i < Input.touchCount; i++) {
                Touch t = Input.GetTouch(i);
    
                if (t.phase == TouchPhase.Began) {
                    if (guiLeft.HitTest(t.position, Camera.main)) {
                        Debug.Log("Touching Left Control");
                        moveLeft = true;
                    }
    
                    if (guiRight.HitTest(t.position, Camera.main)) {
                        Debug.Log("Touching Right Control");
                        moveRight = true;
                    }
    
                    // Are we touching the jump button?
                    if (guiJump.HitTest(t.position, Camera.main)) {
                        Debug.Log("Touching Jump Control");
                        doJump = true;
                    }
                }
    
                if (t.phase == TouchPhase.Ended) {
                    // Stop all movement
                    doJump = moveLeft = moveRight = false;
                    rigidbody2D.velocity = Vector2.zero;
                }
            }
        }
    
        if (Input.GetMouseButtonDown(0)) {
            if (guiLeft.HitTest(Input.mousePosition, Camera.main)) {
                Debug.Log("Touching Left Control");
                moveLeft = true;
            }
    
            if (guiRight.HitTest(Input.mousePosition, Camera.main)) {
                Debug.Log("Touching Right Control");
                moveRight = true;
            }
    
            if (guiJump.HitTest(Input.mousePosition, Camera.main)) {
                Debug.Log("Touching Jump Control");
                doJump = true;
            }
        }
    
        if (Input.GetMouseButtonUp(0)) {
            doJump = moveLeft = moveRight = false;
            rigidbody2D.velocity = Vector2.zero;
        }
    }
    
    void FixedUpdate() {
        if (moveLeft) {
            rigidbody2D.velocity = -Vector2.right * moveSpeed;
        }
    
        if (moveRight) {
            rigidbody2D.velocity = Vector2.right * moveSpeed;
        }
    
        if (doJump) {
            if (rigidbody2D.velocity.y < maxJumpVelocity) {
                rigidbody2D.AddForce(Vector2.up * jumpForce);
            } else {
                doJump = false;
            }
        }
    }
    }
    

    一切都应该正常。我已经测试过了,没有错误。

    【讨论】:

    • Unity 在脚本中看不到错误,但在我的智能手机中,多点触控不起作用:/
    • 您确定您的智能手机支持多点触控并且 GUITexture 位于正确的位置吗?
    • 我的智能手机支持多点触控;)我可以用一根手指移动我的角色,但两根手指不起作用。等待 5 分钟,我认为我的 android SDK 有问题
    • 嗯,当 Unity 制作 APK 文件时,我没有看到 file.apk 和控制台说:img11.hostingpics.net/pics/463093Capture.png
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    相关资源
    最近更新 更多