首页 > 解决方案 > 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);
}

}

标签: if-statementanimationbooleancharacter2d

解决方案


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
}

推荐阅读