[英]Unity - Change direction along x-axis when GetMouseButtonDown
我正在制作一種壁壘式的游戲,當點擊鼠標按鈕時我需要我的角色來改變牆壁。 我使用重力工作,但它產生了不良影響。 因此我現在正在使用transform.position,但現在該字符僅移動一瞬間(我假設transform.position僅在實際單擊鼠標按鈕時激活)。
如何讓它在鼠標點擊上改變方向,而不是僅僅移動一下? 我需要某種while循環,或者我在哪里?
我的課:
//Variables used by the Player
public int flyingSpeed;
bool rightWall = true;
bool inAir = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Constantly moves the Players position along the Y-axis
if (inAir == false) {
if (Input.GetMouseButtonDown (0) && rightWall == true) {
transform.position += Vector3.left * flyingSpeed * Time.deltaTime;
rightWall = false;
inAir = true;
} else if (Input.GetMouseButtonDown (0) && rightWall == false) {
transform.position += Vector3.right * flyingSpeed * Time.deltaTime;
rightWall = true;
inAir = true;
}
}
}
void OnCollisionEnter2D(Collision2D coll) {
inAir = false;
}
Input.GetMouseButtonDown
方法僅在單擊按鈕的第一幀上返回true,因此您的移動操作僅執行一次,這不足以切換牆。
要在牆之間切換,您可以執行以下操作之一:
transform.position
在單擊鼠標時立即移動播放器 制作一個功能,檢查播放器是在右側還是左側(讓我們打電話給WallCheck
)。 然后在rightWall
鼠標時更改rightWall
值。然后將其添加到Update方法中
if (WallCheck() != rightWall) transform.position += rightWall ? Vector3.left : Vector3.right * flyingSpeed * Time.deltaTime;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.