通过触摸跳转 Unity C#

本文关键字:Unity 触摸 | 更新日期: 2023-09-27 18:37:21

我在Unity C#上编写游戏

这是简单的跑步者。

我有 Platformer2DUserControl Script。

就是这样

 using UnityEngine;
using UnitySampleAssets.CrossPlatformInput;
namespace UnitySampleAssets._2D
{
[RequireComponent(typeof (PlatformerCharacter2D))]
public class Platformer2DUserControl : MonoBehaviour
{
    private PlatformerCharacter2D character;
    private bool jump ;
    private void Awake()
    {
        character = GetComponent<PlatformerCharacter2D>();
    }
    private void Update()
    {
        if (Input.touchCount > 0)
            character.Move(1, false, jump);
        if (!jump)
        // Read the jump input in Update so button presses aren't missed.
        jump = CrossPlatformInputManager.GetButtonDown("Jump");
    }
    private void FixedUpdate()
    {
        // Read the inputs.
        bool crouch = Input.GetKey(KeyCode.LeftControl);
       // float h = CrossPlatformInputManager.GetAxis("Horizontal");
        // Pass all parameters to the character control script.
        character.Move(1, false, jump);
        jump = false;
    }
}
}

当我在手机上触摸屏幕时,我尝试让该玩家跳跃。

但它不会跳跃。

我的代码中有什么问题?

谢谢你的帮助。

通过触摸跳转 Unity C#

如果您只想让对象在触摸时跳转,请使用

Input.GetButton("Fire1")

它预定义为鼠标左键单击,也适用于单点触摸输入

Input.GetKeyDown(KeyCode.Space)

据我了解,SPACE也适用于触摸。