(本人第一次用博客)
我在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的。
这不是很明显吗?第一句代码public float speed 5f;就漏写了=,加上=号就好了。public float speed =5f;写代码细心的哦。
你发的代码没有完全在代码块里,看起来有些出入。
_rigidbody2D就是rigidbody2D对吧,_animator就是animator对吧。
我是c#门外汉。
(本人第一次用博客)
我在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的。