-4

我第一次在 Unity 中制作 2D 游戏。我在写剧本,也是第一次,看了一些教程,觉得还不错。但是,我不断收到一条错误消息,我不知道该怎么做。好吧,错误显示在我之前没有显示错误并且一切正常的地方。仅在输入“无效更新”后:

if (Input.GetKeyDown(KeyCode.R))

{
    Attack();
}


void Attack()

{
// Play an attack animation
animator.SetTrigger("Attack");
// Detect enemies in range of attack
// Damage them
}
} 

(我按照教程所说的方式编写了它)突然,当我想看看它是否有效时,我收到一个错误“预期标识符”。错误位于更下方的一行,这里:

@if (grouned)
doubleJump = false;
anim.SetBool ("Grounded", grouned);


if(Input.GetKeyDown(KeyCode.W)&& grouned)

“如果”显示出了问题,所以我在那里添加了一个“@”,但现在它表明在“grouned”这个词之后应该/某事是错误的。我不知道该怎么做。

这里有整个脚本:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Player : MonoBehaviour

{

    public Animator animator;
    public float moveSpeed;
    public float jumpHeight;
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask WhatIsGround;
    private bool grouned; 
    private bool doubleJump;
    
    private Animator anim;



    // Start is called before the first frame update
    void Start(){
    anim = GetComponent<Animator> ();
        
    }
    

    void FixedUpdate(){

    grouned = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, WhatIsGround);



}

    // Update is called once per frame
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.R))
    {
        Attack();
    }
    

    void Attack()

    {
    // Play an attack animation
    animator.SetTrigger("Attack");
    // Detect enemies in range of attack
    // Damage them
    }
    } 

    @if (grouned)
    doubleJump = false;
    anim.SetBool ("Grounded", grouned);


    if(Input.GetKeyDown(KeyCode.W)&& grouned)

    
    {
        GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, jumpHeight);
        }


    if(Input.GetKeyDown(KeyCode.W)&& !grouned && !doubleJump)

    
    {
        GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, jumpHeight);
    doubleJump = true;
        }
    

    if(Input.GetKey(KeyCode.D))
    
    {
    GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
        }


    if(Input.GetKey(KeyCode.A))
    
    {
    GetComponent<Rigidbody2D> ().velocity = new Vector2 (-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
        }

    anim.SetFloat ("Speed", Mathf.Abs (GetComponent<Rigidbody2D> ().velocity.x));
    if(GetComponent<Rigidbody2D>().velocity.x > 0)

    {
    transform.localScale = new Vector3 (3f, 3f, 3f);
    }

    else if (GetComponent<Rigidbody2D>().velocity.x < 0)
    transform.localScale = new Vector3 (-3f, 3f, 3f);

}

    
    
    }
4

2 回答 2

1

看起来你已经把你的Attack函数放在了更新函数的中间,这使得你的花括号没有正确排列,并且在函数之外留下了很多代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour

{

    public Animator animator;
    public float moveSpeed;
    public float jumpHeight;
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask WhatIsGround;
    private bool grouned;
    private bool doubleJump;

    private Animator anim;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
    }

    void FixedUpdate()
    {
        grouned = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, WhatIsGround);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            Attack();
        }

        if(grouned)
            doubleJump = false;
        anim.SetBool("Grounded", grouned);

        if (Input.GetKeyDown(KeyCode.W) && grouned)
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(0, jumpHeight);
        }
        if (Input.GetKeyDown(KeyCode.W) && !grouned && !doubleJump)
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(0, jumpHeight);
            doubleJump = true;
        }
        if (Input.GetKey(KeyCode.D))
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
        }
        if (Input.GetKey(KeyCode.A))
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
        }
        anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
        if (GetComponent<Rigidbody2D>().velocity.x > 0)
        {
            transform.localScale = new Vector3(3f, 3f, 3f);
        }
        else if (GetComponent<Rigidbody2D>().velocity.x < 0)
            transform.localScale = new Vector3(-3f, 3f, 3f);
    }

    void Attack()
    {
        // Play an attack animation
        animator.SetTrigger("Attack");
        // Detect enemies in range of attac
        // Damage them
    }
}

您是否正在使用某种带有语法高亮的编辑器?它将帮助您捕获此类错误以及正确缩进您的代码,这将帮助您发现丢失/额外的{大括号。

于 2021-01-12T11:44:15.127 回答
1

整理完代码后,很明显从 开始的代码@if...不包含在方法中。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public Animator animator;
    public float moveSpeed;
    public float jumpHeight;
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask WhatIsGround;
    private bool grouned;
    private bool doubleJump;

    private Animator anim;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
    }

    void FixedUpdate()
    {
        grouned = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, WhatIsGround);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            Attack();
        }

        void Attack()
        {
            // Play an attack animation
            animator.SetTrigger("Attack");
            // Detect enemies in range of attack
            // Damage them
        }
    }

    @if(grouned)
        doubleJump = false;
        
    anim.SetBool("Grounded", grouned);

    if(Input.GetKeyDown(KeyCode.W)&& grouned)
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2(0, jumpHeight);
    }

    if (Input.GetKeyDown(KeyCode.W) && !grouned && !doubleJump)
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2(0, jumpHeight);
        doubleJump = true;
    }
    
    if (Input.GetKey(KeyCode.D))
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
    }
    
    if (Input.GetKey(KeyCode.A))
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
    }
    
    anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
    if (GetComponent<Rigidbody2D>().velocity.x > 0)
    {
        transform.localScale = new Vector3(3f, 3f, 3f);
    }
    else if (GetComponent<Rigidbody2D>().velocity.x < 0)
        transform.localScale = new Vector3(-3f, 3f, 3f);
    }
}

有可能你的一些右括号}放错了位置。

保持代码格式正确将有助于您识别此错误。良好的格式不仅是好的做法,它有助于阅读代码,也有助于发现这样的错误。

另外,我不确定您希望通过 实现什么@if,这不是剃刀视图/页面!

于 2021-01-12T11:35:19.397 回答