我有一些代碼在一個教程,工作正常,但我不明白爲什麼。代碼是讓一個簡單的2D角色根據它的走向從左到右翻轉。該代碼如何切換布爾工作?
private bool facingright;
void Start() {
//defaults that we are in fact facing right when game starts.
facingright = true;
很明顯,它只是聲明遊戲開始時布爾值爲真。然後,我們創建一個額外的方法來控制,當它是真的還是不是真的:
private void Flip(float horizontal){
//if horizontal is greater than 0 and not facing right.
//or if horizontal is less than zero and is facing right
if (horizontal > 0 && !facingright || horizontal < 0 && facingright) {
//you are no longer facing right, but how does it go back?
facingright = !facingright;
//theScale is transforming the x axis on scale.
Vector3 theScale = transform.localScale;
//the value of the axis change is x * -1
theScale.x *= -1;
//Then change the new value to theScale?
transform.localScale = theScale;
所以,我不能爲我的生命換我的頭周圍爲什麼這個if語句works.You可以在註釋中看到我瞭解它會在這種情況發生或發生其他事情時激活。根據我的理解,這隻會使面對面 - 然而它又如何回到面對面=面向右邊?
這是說朝向=!朝向,但如果這已經是真的,然後扭轉它?
同時,縮放將x軸移動到1或-1。代碼雖然只需要做一次。它如何還原這些更改?任何幫助理解這一點將不勝感激。我認爲這是與這件作品有關的所有代碼。
我想也許你會得到限定詞和作業混淆。另外,1 * -1 = -1。但是,-1 * -1 = 1。這就是它的翻轉方式。 – oxrock
「不正確」是錯誤的。 「不假」是真的。 – Blorgbeard
謝謝你們!所以縮放實際上是翻轉它?在每次翻轉後,翻轉後的縮放會使其循環,或者?等等。 – LocalNerd