c# - 接收关于统一的 } 预期和命名空间问题
问题描述
在统一上制作 2D 平台游戏,我继续收到以下两个错误,这是 C# 的新手,不确定为什么会发生这种情况。
“Assets\PlayerController.cs(15,41): 错误 CS1513: } 预期”
“Assets\PlayerController.cs(70,1):错误 CS1022:类型或命名空间定义,或预期文件结尾”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D rb;
[SerializeField] private Animator anim;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
private enum State {idle, running, jumping}
private State state = State.idle;
}
private void Update()
{
float hdirection = Input.GetAxis("Horizontal");
float vdirection = Input.GetAxis("Vertical");
if (hdirection < 0)
{
rb.velocity = new Vector2(-7, rb.velocity.y);
gameObject.GetComponent<SpriteRenderer>().flipX = true;
}
else if (hdirection > 0)
{
rb.velocity = new Vector2(7, rb.velocity.y);
gameObject.GetComponent<SpriteRenderer>().flipX = false;
}
else
{
}
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, 12);
state = State.jumping;
}
VelocityState();
}
private void VelocityState()
{
if(state == State.jumping)
{
}
else if(Mathf.Abs(rb.velocity.x > Mathf.Epsilon))
{
state = State.running;
}
else
{
state = State.idle;
}
}
}
解决方案
您不能在函数中定义私有变量。枚举和变量状态应该在外面定义。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D rb;
[SerializeField] private Animator anim;
private enum State { idle, running, jumping };
private State state;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
state = State.idle;
}
private void Update()
{
float hdirection = Input.GetAxis("Horizontal");
float vdirection = Input.GetAxis("Vertical");
if (hdirection < 0)
{
rb.velocity = new Vector2(-7, rb.velocity.y);
gameObject.GetComponent<SpriteRenderer>().flipX = true;
}
else if (hdirection > 0)
{
rb.velocity = new Vector2(7, rb.velocity.y);
gameObject.GetComponent<SpriteRenderer>().flipX = false;
}
else
{
}
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, 12);
state = State.jumping;
}
VelocityState();
}
private void VelocityState()
{
if (state == State.jumping)
{
}
else if (Mathf.Abs(rb.velocity.x) > Mathf.Epsilon)
{
state = State.running;
}
else
{
state = State.idle;
}
}
}
推荐阅读
- spartacus-storefront - 后台和斯巴达克斯中货币的一致格式化值
- python - 如何获取从类函数返回的多个数据帧?
- flutter - 我在 Flutter 中遇到了一个问题:在构建 Scaffold 时抛出了以下断言,它给我带来了一个奇怪的问题
- javascript - 如何声明尚未渲染的变量
- javascript - Vue:App.vue 中的标题导航但在不同的视图中进行了修改?
- svelte - 如何替换 Svelte 中目标的内容而不是附加到子项?
- c# - 编辑 ObservableCollection 对象的副本而不编辑 ObservableCollection
- javascript - 如何在打字稿的函数参数中添加默认值?
- javascript - Safari 永远不会跳入这种 if 条件,Chrome 没问题。两者都没有记录错误
- python - 序列化光线未来并稍后返回结果