[英]I made C# script in unity for crouching and i when i press ctrl while I'm crouching it won't stand up
我希望能夠在蹲下時站起來並將其添加到腳本中,但是我這樣做的方式似乎不起作用。 這是我的代碼
using UnityEngine;
public class crouching : MonoBehaviour
{
public bool ctrlPressed = false;
public bool ctrlPressedHalf = false;
public bool crouchTimeRunning = false;
public bool crouchOnCoolDown = false;
public bool crouchAvailable = true;
public float timer = 0.0f;
public float waitTime = 0.0f;
// Update is called once per frame
void Update()
{
ctrlPressed = false;
ctrlPressedHalf = false;
if (crouchTimeRunning)
{
timer += Time.deltaTime;
if (timer >= waitTime)
{
crouchTimeRunning = false;
crouchOnCoolDown = true;
timer = 0.0f;
waitTime = 2.0f;
transform.localScale = new Vector3(10, 20, 10);
transform.position = new Vector3(0, 11, 0);
}
}
if (crouchOnCoolDown)
{
timer += Time.deltaTime;
if (timer >= waitTime)
{
crouchOnCoolDown = false;
crouchAvailable = true;
timer = 0.0f;
waitTime = 0.0f;
}
}
if (ctrlPressedHalf & crouchTimeRunning)
{
crouchTimeRunning = false;
timer = 0.0f;
waitTime = 2.0f;
crouchOnCoolDown = true;
transform.localScale = new Vector3(10, 20, 10);
transform.position = new Vector3(0, 11, 0);
}
if (Input.GetKey(KeyCode.LeftControl))
{
ctrlPressedHalf = true;
}
if (ctrlPressedHalf & crouchAvailable)
{
ctrlPressed = true;
}
if (ctrlPressed)
{
transform.localScale = new Vector3(10, 10, 10);
transform.position = new Vector3(0, 5, 0);
crouchTimeRunning = true;
crouchAvailable = false;
waitTime = 3.0f;
}
}
}
我為此做了這個if
聲明:
if (ctrlPressedHalf & crouchTimeRunning)
{
crouchTimeRunning = false;
timer = 0.0f;
waitTime = 2.0f;
crouchOnCoolDown = true;
transform.localScale = new Vector3(10, 20, 10);
transform.position = new Vector3(0, 11, 0);
}
但是,它不起作用。
這是視頻,您可以從ctrlPressedHalf
變量中看到我正在按下蹲下: https://youtu.be/W4kXkOO15X0
首先,我認為您應該刪除這些變量:
public bool ctrlPressed = false;
public bool ctrlPressedHalf = false;
public bool crouchTimeRunning = false;
public bool crouchOnCoolDown = false;
public bool crouchAvailable = true;
它們在這里似乎都是不必要的。 不過,您將需要一個 boolean:
bool isCrouching = false;
接下來,您將需要一個 if 語句用於蹲下(該語句將在用戶按住左 ctrl 按鈕時執行):
//continue crouching if the player is already crouching and holding the left control button
//timer will affect this if statement if the player is not crouching and clicks the left control button
if(Input.GetKey(KeyCode.LeftControl) && (timer <= 0 || isCrouching))
{
transform.localScale = new Vector3(10, 10, 10);
transform.position = new Vector3(0, 5, 0);
//if I understood correctly, the two lines above were responsible for making a game object crouch
timer = 3f;
isCrouching = true;
}
else
{
isCrouching = false;
timer -= Time.deltaTime;
//whatever code you had there that would make a game object stop crouching
}
您的蹲伏 animation 夾子有退出時間嗎? 我先把它變成假的。 此外,讓你從蹲下到站立的過渡持續時間為零。 如果您還沒有創建任何動畫組件,我建議您這樣做。 這種方式可能會更好。 從動畫師,您將能夠創建不同的動畫。 制作動畫后,Unity 會提供在剪輯期間操縱位置、縮放甚至旋轉的選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.