if-statement - C# Else confusion
问题描述
so i keep gettin an error on (else) and im not to sure what i did wrong, i cant seem to find the problem, please help, im pretty new to coding so heres the entire code i have so far
{
public float speed;
private Rigidbody2D myRigidbody;
private Vector3 change;
private Animator animator;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
change = Vector3.zero;
change.x = Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed;
change.y = Input.GetAxisRaw("Vertical") * Time.deltaTime * speed;
if (change != Vector3.zero)
transform.Translate(new Vector3(change.x, change.y));
MoveCharacter();
UpdateAnimationAndMove();
}
void UpdateAnimationAndMove()
{
{
animator.SetFloat("moveX", change.x);
animator.SetFloat("moveY", change.y);
animator.SetBool("moving", true);
} else {
animator.SetBool("moving", false);
}
}
void MoveCharacter()
{
myRigidbody.MovePosition(transform.position + change.normalized * speed * Time.deltaTime);
}
}
解决方案
You can't use else
, without a corresponding if
before it.
if(condition)
{
// IF condition is true, this gets executed
}
else
{
// ELSE this gets executed
}
推荐阅读
- javascript - Laravel Echo 不监听公共频道事件?
- python - pandas 堆积条形图 - 改变堆积条的边缘颜色
- python - 如何在不编辑字典本身的情况下提取和编辑字典值?
- postgresql - 如何通过从 Jasper 软件工作室的当前日期和旧日期中减去来获得网络天数?
- javascript - 如何在 JavaScript 中部分等待嵌套的 Promise?
- node.js - Foreach 字符串数组,每个都是 upsert 并递增到 1,但在特定字段上保存 2
- swift - 从 JSON 创建日期数组
- r - 对于每个唯一的用户 id,创建一个直方图频率向量 R
- jmeter - 是否可以在不同的时间间隔运行线程-JMeter
- c# - 如何生成一次 Random.Range 值,然后在 void Update 中使用它来移动?统一,C#