【问题标题】:Unity Jumping code for player not working播放器不工作的统一跳转代码
【发布时间】:2020-02-07 16:13:20
【问题描述】:

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

我不确定,但是,我统一创建了一个图像并将其放置在画布中,并使用脚本 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


    【解决方案1】:

    这里有很多东西要解压。

    像 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;
            }
    

    【讨论】:

    • 对不起,我对统一很陌生,所以我通过 youtube 上的教程进行了操作。我制作了两个脚本,因为我正在遵循教程。您能否提供有关如何“重置” OnPointerUp 的示例代码?还是我应该使用不同的方法不再这样做?
    • 我已将示例代码行添加到我的解决方案中。话虽如此,您可以考虑查看 Brackey 的 2D 运动教程,了解最佳实践角色控制器。
    • 我添加了该行,但我不知道为什么,它仍然没有工作
    • 您是否从您的 Joybutton 中删除了 OnPointerUp 代码?
    • 哦,对不起,我打错了,我再次检查,它有效。非常感谢
    【解决方案2】:

    如何避免玩家在空中跳跃两次?

    private void FixedUpdate()
    {
        rb.velocity = new Vector3(
                      joystick.Horizontal * 5f, rb.velocity.y, 
                      joystick.Vertical * 5f);
    
        if (!jump && joybutton.Pressed)
        {
            jump = true;
            rb.velocity += Vector3.up * 10f;
        }
    
        if (jump && !joybutton.Pressed)
        {
            jump = false;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多