【问题标题】:Unity Jumps at Different HeightsUnity 在不同的高度跳跃
【发布时间】:2014-09-22 23:06:13
【问题描述】:

我正在为移动设备制作 2D 平台游戏。我有一个 guiTexture,当按下它时,我的精灵会跳跃。但是每当我按下 guiTexture 时,精灵每次都会跳到不同的高度,这取决于我是按住按钮还是轻轻按下它。

这是我正在使用的跳转脚本:

    using UnityEngine;
using System.Collections;

public class Jump : MonoBehaviour {

    public GUITexture jumpButton;

    bool grounded = false;
    public Transform groundCheck;
    float groundRadius = 0.2f;
    public LayerMask whatIsGround;

    public float jumpForce = 700f;

    public GameObject character;

    void Start () {

    }

    void Update () {

    }

    void FixedUpdate () {
        grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);

        if (grounded && jumpButton.HitTest(Input.GetTouch(0).position)) {
            rigidbody2D.AddForce(new Vector2(0, jumpForce));
        }
    }
}

【问题讨论】:

    标签: mobile unity3d unity3d-2dtools


    【解决方案1】:

    如果触摸被按下,让你的 if 语句检查 touchphase,然后它只会在你按下时触发一次

    if (grounded && jumpButton.HitTest(Input.GetTouch(0).position) && Input.GetTouch(0).phase == TouchPhase.Began) {
            rigidbody2D.AddForce(new Vector2(0, jumpForce));
        }
    

    【讨论】:

      猜你喜欢
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      相关资源
      最近更新 更多