【问题标题】:DoubleJumping Limits in Unity2DUnity 2D 中的双跳极限
【发布时间】:2017-04-20 14:44:41
【问题描述】:

查看有关 Unity 的帮助论坛,我很快发现我正在查看的语法确实已经过时了(同样的事情在这里:Unity Doublejump in C#)。

这是我正在谈论的文章: http://answers.unity3d.com/questions/753238/restrict-number-of-double-jumps.html

例如,在 void Awake() 中,在我正在使用的当前版本的 Unity 中,它表示rigidbody2D.fixedAngle = true;不再受支持,我需要在我试图编程的游戏对象上使用约束(我应该使用什么轴...... x、y 或 z?)。在进行了一些编辑并查看了错误消息之后,我能够将所有刚体2D.velocity 更改为更新后的语法,即 GetComponent ().velocity 。

这是我的代码:

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

    public class NewBehaviourScript : MonoBehaviour {

public float speed = 6.0f;
//public float j
Transform groundCheck;
//private float overlapRadius = 0.2f;
public LayerMask whatisGround;
private bool grounded = false;
private bool jump = false;
public float jumpForce = 700f;
private bool doubleJump = false;
public int dJumpLimit = 5;

void Start()
{
    groundCheck = transform.Find ("groundcheck");
    PlayerPrefs.SetInt ("doublejumps", dJumpLimit);
}

void Update()
{
    if (Input.GetKey(KeyCode.Space)) 
    {
        jump = true;
    }
    //perhaps put A and D here?
}
void FixedUpdate()
{
    //to check if Mario is on the ground

    //overlap collider replace Overlap Circle???
    //overlap point??
    //grounded = GetComponent<Rigidbody2D> ().OverlapCollision(groundCheck.position, overlapRadius, whatisGround);

    if (grounded)
        doubleJump = false;

    if (dJumpLimit < 1)
        doubleJump = true;

    bool canJump = (grounded || !doubleJump);

    if (jump && canJump) {
        GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, 0);
        GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, jumpForce));

        if (!doubleJump && !grounded) {
            doubleJump = true;
            dJumpLimit--;

        }

    }

        jump = false;


        //code that will work with the limits?
        //GetComponent<Rigidbody2D>().velocity = new Vector2(speed,GetComponent<Rigidbody2D>().velocity.y);

        //this will make it stack?
        //GetComponent<Rigidbody2D>().velocity = new Vector2(speed,GetComponent<Rigidbody2D>().velocity.x);
        //GetComponent<Rigidbody2D>().velocity = new Vector2(speed,GetComponent<Rigidbody2D>().velocity.y);
    }

}

好在它最终能够编译。但我仍然不知道约束是如何工作的(它们抵抗 x、y、z 轴上的运动,对吧?)。跳跃仍然没有顶点,变量 dJumpLimit 似乎并没有停止所有的跳跃!我在试图破译布尔值试图完成什么时也遇到了很多麻烦,如果你告诉我过时的代码试图做什么,以及我这样做失败的原因,这将有很大帮助。这对我有很大帮助。非常感谢您的帮助!!!

【问题讨论】:

  • 当马里奥在地上时,接地为真,当玩家按下空格键时,跳跃为真,如果马里奥被接地或还没有刚刚二段跳,则 canJump 为真。由于您注释掉了接地检查,因此 doubleJump 将始终是错误的,这意味着您始终可以双跳。

标签: c# unity3d


【解决方案1】:

我已经添加了相同的代码和一些额外的 cmets 来帮助你。

基本思想是在某些条件下允许跳跃,在某些其他条件下允许二段跳,

试着从逻辑上想一想,马里奥只有在地上时才能跳跃,所以我们需要检查每一帧。如果他被接地,并且玩家按下空格键,我们会让他跳跃。

现在,我们希望马里奥能够进行二段跳,但前提是他已经在空中(否则这只是一个常规的跳跃),并且前提是他还没有在这个“当前”跳跃中二段跳。

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

public class NewBehaviourScript : MonoBehaviour {

    public float speed = 6.0f;
    //public float j
    Transform groundCheck;  // For checking if grounded
    //private float overlapRadius = 0.2f;  // For checking if grounded
    public LayerMask whatisGround;  // A layer mask to distinguish what is considered as ground
    private bool grounded = false;  // True when Mario is touching ground
    private bool jump = false;      // Flag to check when player intends to jump
    public float jumpForce = 700f;  // How much force we will apply to our jump
    private bool doubleJump = false;  // This becomes true once we have double-jumped
    public int dJumpLimit = 5;  // A limit to prevent too many jumps happening

    void Start()
    {
        // Find the Transform called "groundcheck"
        groundCheck = transform.Find ("groundcheck"); 
        // Set the PlayerPrefs integer called "doublejumps" to the value of dJumpLimit
        PlayerPrefs.SetInt ("doublejumps", dJumpLimit);
    }

    void Update()
    { 
        // If Space is being pressed...
        if (Input.GetKey(KeyCode.Space)) 
        {
            jump = true;  // Set jump bool to true to indicate our intention to jump
        }
    //perhaps put A and D here?
    }

    void FixedUpdate()
    {
        //to check if Mario is on the ground - this is necessary so we can't jump forever

        //overlap collider replace Overlap Circle???
        //overlap point??
        //grounded = GetComponent<Rigidbody2D> ().OverlapCollision(groundCheck.position, overlapRadius, whatisGround);

        // If Mario is touching ground...
        if (grounded)
            doubleJump = false;  // Make sure that as we are grounded we are not allowed to "jump again"

        if (dJumpLimit < 1)
            doubleJump = true;
        // Set a new bool to true if grounded is true OR doubleJump is false
        bool canJump = (grounded || !doubleJump);

        // If the player pressed space AND we are allowed to jump...
        if (jump && canJump) {
            // Apply existing x velocity to the x direction 
            GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, 0);
            // Apply our jump force to the y direction 
            GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, jumpForce));

        // If doubleJump is false AND we are not grounded...
        if (!doubleJump && !grounded) {
            doubleJump = true;  // We have double jumped so set to true
            dJumpLimit--;  // Decrement one from dJumpLimit
        }
    }

        jump = false;  // Reset the jump bool to false

        //code that will work with the limits?
        //GetComponent<Rigidbody2D>().velocity = new Vector2(speed,GetComponent<Rigidbody2D>().velocity.y);

        //this will make it stack?
        //GetComponent<Rigidbody2D>().velocity = new Vector2(speed,GetComponent<Rigidbody2D>().velocity.x);
        //GetComponent<Rigidbody2D>().velocity = new Vector2(speed,GetComponent<Rigidbody2D>().velocity.y);
    }
}

【讨论】:

  • 所以它现在可以跳到极限了,但它只会跳一次。你能解释一下我如何用 Djumplimits 解决这个问题吗?非常感谢!
猜你喜欢
  • 1970-01-01
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多