查看關於Unity的幫助論壇,我很快發現我所看到的語法確實過時了(同樣的事情在這裏:Unity Doublejump in C#)。Unity2D中的DoubleJumping限制
下面是我在談論的文章: http://answers.unity3d.com/questions/753238/restrict-number-of-double-jumps.html
例如,在虛空中清醒(),在Unity的當前版本我使用的,它說,rigidbody2D.fixedAngle = TRUE;不再受支持,我需要對我正在嘗試編程的gameObject使用約束(我應該使用哪個軸... x,y或z?)。在完成一些編輯之後,查看錯誤消息後,我可以將所有的rigidbody2D.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將永遠是假的,這意味着你總是可以雙跳。 –
非常感謝! –