[英]Unity crouching
if(Input.GetKeyDown(crouchKey) && isCrouching == false )
{
YCrouched();
}
if (Input.GetKeyUp(crouchKey) && isCrouching == true && isInCeiling == false)
{
NCrouched();
}
}
void YCrouched()
{
isCrouching = true;
controller.height = 1f;
player.gameObject.transform.localScale = new Vector3(0.8f, crouchHeight, 0.8f);
moveSpeed = crouchSpeed;
}
void NCrouched()
{
isCrouching = false;
controller.height = 2f;
player.gameObject.transform.localScale = new Vector3(1, 1, 1);
moveSpeed = deafultSpeed;
}
所以問題是,當我從上面有天花板的通風口之類的區域出來時,我仍然蹲着。 只有當我再次按下蹲下時,我才會回來。
執行此操作的一種方法可能是檢查 inCeiling 狀態是否已從先前的更新更改。 嘗試添加以下內容
//keeps the previous state of isInCeiling, add this to wherever isInCeiling is defined.
bool wasInCeiling
//add this check to your list of if-checks
if(wasInCeiling == true && isInCeiling == false)
{
NCrouched() ;
}
這應該讓玩家在他從天花板出來時自動站起來。
此外,您需要在更新代碼中添加一些內容,以便在每次更新時首先將當前 isInCeiling 值設置為 wasInCeiling,然后檢查 isInCeiling 的當前狀態。 那應該始終將 wasInCeiling 保持在 isInCeiling 后面一幀。
if (Input.GetKeyUp(crouchKey) && isCrouching == true && isInCeiling == false)
{
NCrouched();
}
改成
if (isCrouching == true && isInCeiling == false)
{
NCrouched();
}
假設您正在跟蹤 isInCeilling
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.