(本人第一次用博客)
我在Unity里写了一个C#脚本,希望它能控制角色左右移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystems;
public class Player: MonoBehaviour
{
public float speed 5f;
private Rigidbody2D _rigidbody2D;
private Animator _animator;
private float x;
private float y;
void Start()
{
rigidbody2D = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
private void Run()
{
Vector3 movement = new Vector3(x, y, 0);
rigidbody2D.transform.position += movement * speed * Time.deltaTime;
}
void Update()
{
x = Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
if(x>0)
{
rigidbody2D.transform.eulerAngles = new Vector3(0f,0f,0f);
animator.SetBool("run",true);//run是一个用来判断角色是否在移动的变量
}
else if(x<0)
{
rigidbody2D.transform.eulerAngles = new Vector3(0f, 180f, 0f);
animator.SetBool("run", true);
}
else
{
animator.SetBool("run", false);
}
Run();
}
}
可是Unity报错了:error CS1003: Syntax error, ',' expected
我的代码检查了很多遍,应该是没有问题。
请各位看一看,谢谢!
对了,我的编译器是Vs2022 Unity用的也是2022的。