首页 > 解决方案 > 播放器不工作的统一跳转代码

问题描述

跳跃按钮在我的统一中不起作用,因为我的玩家没有跳跃。我遵循了一个教程,但它似乎不起作用。我的代码一定有问题,但我似乎无法解决,因为没有错误。

我不确定,但是,我统一创建了一个图像并将其放置在画布中,并使用脚本 Joybutton.cs 将其命名为“Fixed Joybutton”,但是当我运行它并尝试单击它时,它不会让我的玩家跳跃。我认为问题是“joybutton = FindObjectOfType();” 并将其更改为“joybutton = GetComponent();” 但仍然无法正常工作。

这是跳转按钮本身的代码,Joybutton.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
{
    [HideInInspector]
    public bool Pressed;

    // Start is called before the first frame update
    void Start()
    {

    }

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

    }

    public void OnPointerDown(PointerEventData eventData)
    {
        Pressed = true;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        Pressed = false;
    }
}

// and this is the code that is in my player, PlayerController.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{


    protected Joystick joystick;
    protected Joybutton joybutton;
    protected bool jump;
    public float speed;
    public Text playerDisplay;
    public Text scoreDisplay;
    public bool isFlat = true;

    private Rigidbody rb;
    public static int count = 0;

    private void Start()
    {
        joystick = FindObjectOfType<Joystick>();
        joybutton = FindObjectOfType<Joybutton>();//this is to find the object, I changed it to "joybutton = GetComponent<Joybutton>();" but still doesnt work
        rb = GetComponent<Rigidbody>();

    }



    private void FixedUpdate()
    {

        rb.velocity = new Vector3(joystick.Horizontal * 5f, rb.velocity.y, joystick.Vertical * 5f); // joystick to move my player, its working

        if (!jump && joybutton.Pressed)// this is for the jump, that is not working
        {
            jump = true;
            rb.velocity += Vector3.up * 10f;
        }

        if (jump && !joybutton.Pressed)
        {
            jump = false;
        }


    }


    public void OnTriggerEnter(Collider other)
    {
        GameObject gameControl = GameObject.Find("Timer");//name of my gameobject
        Loading loading = gameControl.GetComponent<Loading>();
        if (other.gameObject.CompareTag("Pick Up"))
        {
            other.gameObject.SetActive(false);
            count = count + 1;
            Awake();
        }
        else if (other.gameObject.CompareTag("Time Boost"))
        {
            other.gameObject.SetActive(false);
            loading.sec += 10;
            if (loading.sec > 60)
            {
                int sec1 = loading.sec - 60;
                loading.sec = sec1;
                loading.minutes ++;
            }

            Awake();
        }
    }

    private void Awake()
    {
        if (DBManager.username == null)
        {
            UnityEngine.SceneManagement.SceneManager.LoadScene(0);
        }
        playerDisplay.text = "Player: " + DBManager.username;//display username
        scoreDisplay.text = "Score: " + count.ToString();//display score 
    }

}


//Player is moving but cannot jump. Please show me the error in my code and solution. Thank you so much.

标签: c#unity3d

解决方案


这里有很多东西要解开。

像 OnPointerDown 这样的 UI 事件发生在帧周期上。FixedUpdate 发生在固定周期。您很有可能拥有非常高的帧速率,导致每个固定周期有 5-6 帧。您的joybutton 将仅设置为“按下”一帧,因此joybutton 很有可能在跳转发生之前触发 OnPointerUp。

解决此问题的最快方法是不使用 OnPointerUp“重置”joybutton 的按下状态,而是在 PlayerController 中处理跳转时重置它。话虽如此,你能帮我理解为什么这两个脚本是分开的吗?

与其在 JoyButton 脚本中将 .Pressed 状态设置为 false,不如在您在 PlayerController 中使用它之后将其重置:

        if (!jump && joybutton.Pressed)// this is for the jump, that is not working
        {
            jump = true;
            joybutton.Pressed = false; //Add this line of code
            rb.velocity += Vector3.up * 10f;
        }

推荐阅读