Unity3d 编译器生成;错误CS1513:}应为“预期”;,但是}有吗
我正在学习一个教程,在那里我在Unity中构建了我的第一个2D游戏 我添加了一个flipX功能,在我改变方向(按a或D)时翻转精灵 我尝试使用相同的脚本,并尝试编译它。有两个错误: Assets\Scripts\Player.cs(83,25):错误CS1002:;预期 Assets\Scripts\Player.cs(83,25):错误CS1513:} 我知道他们的意思,检查了我的剧本。我肯定他们在那里,他们不在那里。我就是想不出这个错误Unity3d 编译器生成;错误CS1513:}应为“预期”;,但是}有吗,unity3d,compiler-errors,Unity3d,Compiler Errors,我正在学习一个教程,在那里我在Unity中构建了我的第一个2D游戏 我添加了一个flipX功能,在我改变方向(按a或D)时翻转精灵 我尝试使用相同的脚本,并尝试编译它。有两个错误: Assets\Scripts\Player.cs(83,25):错误CS1002:;预期 Assets\Scripts\Player.cs(83,25):错误CS1513:} 我知道他们的意思,检查了我的剧本。我肯定他们在那里,他们不在那里。我就是想不出这个错误 using System.Collections; u
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody2D _rigid;
[SerializeField]
private float _jumpForce = 5.0f;
private bool _resetJump = false;
[SerializeField]
private float _speed = 3.5f;
private PlayerAnimation _playerAnim;
private SpriteRenderer _playerSprite;
// Start is called before the first frame update
void Start()
{
_rigid = GetComponent<Rigidbody2D>();
_playerAnim = GetComponent<PlayerAnimation>();
_playerSprite = GetComponentInChildren<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
Movement();
}
void Movement()
{
float horizontalInput = Input.GetAxisRaw("Horizontal");
if (horizontalInput > 0)
{
Flip(true);
}
else if (horizontalInput < 0)
{
Flip(false);
}
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded() == true)
{
Debug.Log("Jump!");
_rigid.velocity = new Vector2(_rigid.velocity.x, _jumpForce);
StartCoroutine(ResetJumpRoutine());
}
_rigid.velocity = new Vector2(horizontalInput * _speed, _rigid.velocity.y);
_playerAnim.Move(horizontalInput);
}
bool IsGrounded()
{
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector2.down, 0.6f, 1 << 8);
if (hitInfo.collider != null)
{
if (_resetJump == false)
return true;
}
return false;
}
void Flip(bool faceRight)
{
if (faceRight == true)
{
_playerSprite.flipX = false;
}
else if (faceRight == false)
{
_playerSprite,flipX = true;
}
}
IEnumerator ResetJumpRoutine()
{
_resetJump = true;
yield return new WaitForSeconds(0.1f);
_resetJump = false;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家:单一行为
{
私有刚性模块2d_刚性;
[序列化字段]
专用浮子_jumpForce=5.0f;
私有bool_resetJump=false;
[序列化字段]
私人浮子_速度=3.5f;
私人游戏者形象(playerAnim),;
私人精灵者;
//在第一帧更新之前调用Start
void Start()
{
_刚性=GetComponent();
_playerAnim=GetComponent();
_playerSprite=getComponentChildren();
}
//每帧调用一次更新
无效更新()
{
运动();
}
空位移动()
{
float horizontalInput=Input.GetAxisRaw(“水平”);
如果(水平输入>0)
{
翻转(真);
}
否则如果(水平输入<0)
{
翻转(假);
}
if(Input.GetKeyDown(KeyCode.Space)&&isground()==true)
{
Log(“跳转!”);
_刚性速度=新矢量2(_刚性速度x,_跳跃力);
start例程(ResetJumpRoutine());
}
_刚性速度=新矢量2(水平输入*_速度,_刚性速度.y);
_playerAnim.Move(水平输入);
}
bool-IsGrounded()
{
RaycastHit2D hitInfo=Physics2D.Raycast(transform.position,Vector2.down,0.6f,1这是一个简单的错误,将\u playerSprite,flipX=true;
替换为\u playerSprite.flipX=true;
。
您有一个逗号而不是一个点。如果您在代码中添加一条注释,指出哪一行导致了错误,这会有所帮助,因此我们不必计算行数来查找第85行。代码编辑器高亮显示该行,以便您很容易看到,但不幸的是,我们无法从这里看到您的屏幕。\u playerSprite,flipX=true;
有一个,
而不是
…请注意,您可以简化整个方法,如void Flip(bool faceRigt){u playerSprite.flipX=!faceRight;}