点击跳转问题

时间:2018-01-11 20:43:44

标签: c# unity3d touch

问题

所以我在iOS和Android应用程序商店发布了名为Jumpol的游戏,它运行正常。唯一的问题是跳跃机制有点时髦。有时当我和其他玩家点击时,我的玩家有时会跳跃而其他时候不会跳跃。此外,当您点击侧面附近的屏幕时,它有时不会跳跃。有没有人有任何建议?

游戏链接(如果您想亲眼看看)

Android Link

iOS Link

C#代码

using UnityEngine;

public class Player : MonoBehaviour {

    Rigidbody rb;

    float speed = 640;              // Speed of player.
    float thrust = 40;              // Jump force.
    float gravity = -6.18f;         // Gravity force.

    bool isGrounded = false;        // If on ground or not.

    private void Start () {

        rb = GetComponent<Rigidbody> ();

    }

    private void Update () {

        if (Application.platform == RuntimePlatform.WindowsEditor) DesktopControls ();

        if (Application.platform == RuntimePlatform.IPhonePlayer ||
            Application.platform == RuntimePlatform.Android) MobileControls ();

    }

    private void FixedUpdate () {

        if (GameManager.Instance.started) {

            Move ();
            Gravity ();

        }

    }

    private void OnCollisionEnter (Collision collision) {

        if (collision.gameObject.tag == "Ground") {
            isGrounded = true;
        }

    }

    private void OnCollisionExit (Collision collision) {

        if (collision.gameObject.tag == "Ground") {
            isGrounded = false;
        }

    }

    /// <summary>
    /// Controls for mobile devices.
    /// </summary>
    private void MobileControls () {

        if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) Jump ();

    }

    /// <summary>
    /// Controls for desktops.
    /// </summary>
    private void DesktopControls () {

        if (Input.GetMouseButtonDown (0)) Jump ();

    }

    /// <summary>
    /// Makes player jump.
    /// </summary>
    private void Jump () {

        if (instructions.activeSelf) {
            instructions.SetActive (false);
        }

        if (isGrounded) {
            rb.velocity += new Vector3 (0, thrust, 0);

            SoundManager.Instance.PlayOneShot (SoundManager.Instance.jump);
        }

    }

    /// <summary>
    /// Adds gravity.
    /// </summary>
    private void Gravity () {

        rb.velocity += new Vector3 (0, gravity, 0);

    }

    /// <summary>
    /// Moves player.
    /// </summary>
    private void Move () {

        rb.velocity = new Vector3 (rb.velocity.x, rb.velocity.y, speed * Time.deltaTime);

    }

}

1 个答案:

答案 0 :(得分:2)

  

有时当我和其他玩家点击时,我的播放器有时会跳跃而其他时间不会跳跃。

您的代码在很大程度上依赖于碰撞事件来实现您的isGrounded状态。哪个不会让我错,Unity Physics系统是一个功能强大且功能强大的系统,但是,在Unity物理引擎中,在运行时模拟的刚体对象和碰撞有时会产生不完美的结果。它在实践中更像是近似值,因此这些近似值并不总是精确的。

    private void OnCollisionEnter (Collision collision) {

    if (collision.gameObject.tag == "Ground") {
        isGrounded = true;
    }

}

private void OnCollisionExit (Collision collision) {

    if (collision.gameObject.tag == "Ground") {
        isGrounded = false;
    }

}

我建议不要在isGroundedOnCollisionEnter中更改OnCollisionExit状态,而是进行简短的Raycast检查。您的玩家刚体对象可以轻松地从OnCollisionEnterOnCollisionExit移动,从而导致isGround bool在true移动false时移动到ground标记对象,当您希望它始终接地时,会导致jump调用被忽略,以为您处于空中状态。我建议实施更可靠的地面检查系统。

有关如何实施Raycast based platformer system here is an example from Unity themselves with video and code in the link and all:

的示例
void Update () 
{
    grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));

    if (Input.GetButtonDown("Jump") && grounded)
    {
        jump = true;
    }
}

它最初是用于2D的,所以有Physics2D函数调用,但是,逻辑是相同的,你需要将2D调用换成它们的3D等价物。 (例如Physics.Raycast代替Physics2D.Linecast

  

当您点击两侧附近的屏幕时,它有时也不会跳跃。

至于此,我想说它与我上面提到的问题有关。查看iOS App产品页面上的屏幕截图,我没有看到任何可能阻碍触摸输入事件的方式的UI。