簡體   English   中英

角色在第一次跳躍時跳得非常高,然后正常跳躍。 我該如何解決? (統一二維)

[英]The character jumps super high at it's first jump, then it jumps normally. How can I fix it? (Unity 2D)

所以這基本上就是這里的問題。 我正在使用物理學創建一個跳轉腳本。 這是代碼。 關於如何解決它的任何想法? 我還沒有嘗試任何重要的事情......只是一些小的變化,因為我不知道該怎么做。

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

public class Movement : MonoBehaviour
{
bool canjump = false;
void Start()
{

}
void Update()
{
    if (Input.GetKey("left"))
    {
        gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(-40000 * Time.deltaTime, 0));
    }
    if (Input.GetKey("right"))
    {
        gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(40000 * Time.deltaTime, 0));
    }
    if (Input.GetKeyDown("up") && canjump == true)
    {
        gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 4000000 * Time.deltaTime));
    }
}
void OnTriggerEnter2D()
{
    canjump = true;
}
void OnTriggerExit2D()
{
    canjump = false;
}

}

PD:調用 OnTriggerEnter && Exit 是因為我將觸發器放在平台上方。 對不起我的英語,我不是英語:P 在此先感謝。

對於初學者,您應該將物理內容放在 FixedUpdate() 中,並在 Update() 中輸入,這是未經測試的:

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

public class Movement : MonoBehaviour
{
    bool jump = false;
    bool canjump = false;
    void Start()
    {

    }

    void FixedUpdate() {
        if(jump) 
        {
            gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 4000000 * Time.deltaTime));
             jump = false;
       }
    }

    void Update()
    {
        if (Input.GetKey("left"))
        {
            gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(-40000 * Time.deltaTime, 0));
        }
        if (Input.GetKey("right"))
        {
            gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(40000 * Time.deltaTime, 0));
        }
        if (Input.GetKeyDown("up") && canjump == true)
        {
             jump = true;
        }
    }
    void OnTriggerEnter2D()
    {
        canjump = true;
    }
    void OnTriggerExit2D()
    {
        canjump = false;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM