C# 画布的Unity中的跳转按钮

C# 画布的Unity中的跳转按钮,c#,button,unity3d,C#,Button,Unity3d,我正在寻找C#或Java中的代码或脚本,以使我的多维数据集在下面的脚本中标记为Playerjump 我写了一些代码并将其附加到画布上的一个按钮上,但问题是当我按住按钮时,它会不断跳跃,并进行无限高的跳跃 这是我用C写的脚本# 你可以用一个小盒子 在例行程序开始时,您设置(例如)“isJumping”(bool),然后在开始循环位之前,您可以通过检查“isJumping”来检查您是否正在跳跃 如果不是“isJumping”,则将isJumping设置为true,进行跳跃,然后在完成例行程序后,将i

我正在寻找C#或Java中的代码或脚本,以使我的多维数据集在下面的脚本中标记为
Player
jump

我写了一些代码并将其附加到画布上的一个按钮上,但问题是当我按住按钮时,它会不断跳跃,并进行无限高的跳跃

这是我用C写的脚本#

你可以用一个小盒子

在例行程序开始时,您设置(例如)“isJumping”(bool),然后在开始循环位之前,您可以通过检查“isJumping”来检查您是否正在跳跃

如果不是“isJumping”,则将isJumping设置为true,进行跳跃,然后在完成例行程序后,将isJumping设置为false

//untested (uncompiled) code written on the fly
bool isJumping = false;
IEnumerator doJump()
{
    if (!isJumping) {
        isJumping = true;
        // do jump (probably a loop)
        while (jumpCondition) {
            // jump instructions
            yield return
        }
        //unset isJumping, inside 'if' but after yield return 
        isJumping = false
    }
}

注意:协同程序中
屈服返回后的代码将只(可能)运行一次,并且只在协同程序存在时运行(因为不再屈服意味着协同程序结束)

钩住
指针向下(按下按钮时调用)和
指针向下(按钮再次松开)UIButton中的事件,并使用
Time.deltaTime
对位置的平移进行加权,您应该可以开始了。(
player.transform.Translate(0,1*Time.deltaTime,0)
,可以选择将其与另一个速度调制因子相乘。)参考:

编辑:是的,一些示例代码。首先,我在按钮上有一个
EventTrigger
组件。我使用它,这样我就可以像前面描述的那样钩住
PointerDown
PointerUp
事件。在inspector中看起来是这样的:

(使用“添加新事件类型”按钮重定向事件调用。) 然后,我在按钮上有这个脚本。代码本身就说明了问题

using UnityEngine;
using UnityEngine.EventSystems;

public class JumpButton : MonoBehaviour {

    private bool shouldJump = false;

    // Update is called once per frame
    void Update () {
        //Find the player
        var player = GameObject.FindGameObjectWithTag("Player");
        //No player? exit out.
        if (player == null)
            return;
        //Is the jump button currently being pressed?
        if (shouldJump)
        {
            //Translate it upwards with time.
            player.transform.Translate(new Vector3(0, Time.deltaTime * 5, 0));
            //Make sure the Rigidbody is kinematic, or gravity will pull us down again
            if (player.GetComponent<Rigidbody>().isKinematic == false)
                player.GetComponent<Rigidbody>().isKinematic = true;
        }
        //Not jumping anymore? reset the Rigidbody.
        else
            player.GetComponent<Rigidbody>().isKinematic = false;
    }

    //When the button is being pressed down, this function is called.
    public void ButtonPressedDown(BaseEventData e)
    {
        shouldJump = true;
    }

    //When the button is released again, this function is called.
    public void ButtonPressedUp(BaseEventData e)
    {
        shouldJump = false;
    }
}
使用UnityEngine;
使用UnityEngine.EventSystems;
公共类JumpButton:MonoBehavior{
私有bool shouldJump=false;
//每帧调用一次更新
无效更新(){
//找到玩家
var player=GameObject.FindGameObjectWithTag(“player”);
//没有玩家?退出。
if(player==null)
返回;
//当前是否正在按下跳转按钮?
如果(应该跳转)
{
//随时间向上翻译。
player.transform.Translate(新向量3(0,Time.deltaTime*5,0));
//确保刚体是运动的,否则重力会把我们拉下来
if(player.GetComponent().iskinetic==false)
player.GetComponent().iskinetic=true;
}
//不再跳了?重置刚体。
其他的
player.GetComponent().iskinetic=false;
}
//按下按钮时,调用此函数。
公共无效按钮按下(BaseEventData e)
{
shouldJump=true;
}
//再次释放按钮时,调用此函数。
公共无效按钮按下(BaseEventData e)
{
shouldJump=false;
}
}
不过,切换到运动学刚体是可选的。还可以使用
Translate()
调用中的乘法常量调整速度。我用一个标准的立方体测试了这段代码,这个立方体上有
Player
标记和
刚体,工作正常


快乐编码

你能在我的脚本中编辑吗?…我的意思是使它正确…我会试试看
using UnityEngine;
using UnityEngine.EventSystems;

public class JumpButton : MonoBehaviour {

    private bool shouldJump = false;

    // Update is called once per frame
    void Update () {
        //Find the player
        var player = GameObject.FindGameObjectWithTag("Player");
        //No player? exit out.
        if (player == null)
            return;
        //Is the jump button currently being pressed?
        if (shouldJump)
        {
            //Translate it upwards with time.
            player.transform.Translate(new Vector3(0, Time.deltaTime * 5, 0));
            //Make sure the Rigidbody is kinematic, or gravity will pull us down again
            if (player.GetComponent<Rigidbody>().isKinematic == false)
                player.GetComponent<Rigidbody>().isKinematic = true;
        }
        //Not jumping anymore? reset the Rigidbody.
        else
            player.GetComponent<Rigidbody>().isKinematic = false;
    }

    //When the button is being pressed down, this function is called.
    public void ButtonPressedDown(BaseEventData e)
    {
        shouldJump = true;
    }

    //When the button is released again, this function is called.
    public void ButtonPressedUp(BaseEventData e)
    {
        shouldJump = false;
    }
}