【发布时间】:2017-10-20 23:55:41
【问题描述】:
我有一些遵循教程的代码可以正常工作,但我不明白为什么。代码是让一个简单的 2D 角色根据行进的方向从左向右翻转。
private bool facingright;
void Start () {
//defaults that we are in fact facing right when game starts.
facingright = true;
很明显,它只是说明在游戏开始时 bool 为真。然后我们创建一个额外的方法来控制它何时为真或不为真:
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 语句有效。你可以在 cmets 中看到,我知道如果发生这种情况或发生其他事情,它就会激活。根据我的理解,这只会使 facesright = !facingright - 但它又如何回到 !facingright = faces right ?
是不是说 facesright = ! facesright,但如果这已经是真的,那么反过来呢?
同时,缩放将 x 轴移动到 1 或 -1。该代码虽然只要求执行一次。它如何也恢复更改?任何有助于理解这一点的帮助将不胜感激。我想这就是这块相关的所有代码了。
【问题讨论】:
-
我认为您可能对限定词和作业感到困惑。此外,1 *-1 = -1。但是,-1 * -1 = 1。这就是它的翻转方式。
-
“不正确”是错误的。 “不假”是真的。
-
谢谢大家!所以缩放实际上是在翻转它?每次翻转后,缩放反转翻转使其循环,或者?废话。
-
另一种方法是:
facingright = horizontal > 0;(这意味着如果水平大于零,则向右为真,否则为假)。
标签: c# unity3d game-physics