我第一次在 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);
}
}