Unity3d 字符不';不要使用IEnumerator移动精确单位
我正在做一个像地铁冲浪者一样的游戏。我有3条球员可以移动的车道。 问题是,当我按下移动控制键时,角色移动略多于1个单位,如果我从-1移动到0,我的角色将处于0.02或类似的位置。 因此,在点击移动控制A和D 10-15次后,我的角色不在车道上,并且X=1.2、0.3、-0.8等等。。。。。。。。 跳转控件也存在此问题。 如何解决这个问题。。。。 我对unity非常熟悉。这几天刚开始。 请帮帮我。 我的编码是Unity3d 字符不';不要使用IEnumerator移动精确单位,unity3d,Unity3d,我正在做一个像地铁冲浪者一样的游戏。我有3条球员可以移动的车道。 问题是,当我按下移动控制键时,角色移动略多于1个单位,如果我从-1移动到0,我的角色将处于0.02或类似的位置。 因此,在点击移动控制A和D 10-15次后,我的角色不在车道上,并且X=1.2、0.3、-0.8等等。。。。。。。。 跳转控件也存在此问题。 如何解决这个问题。。。。 我对unity非常熟悉。这几天刚开始。 请帮帮我。 我的编码是 private string lanechange = "n"; privat
private string lanechange = "n";
private string jumpcontrol = "n";
void Start()
{
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 3);
}
void Update()
{
if((Input.GetKey("a")) && (lanechange=="n") && (transform.position.x>-.9f) )
{
GetComponent<Rigidbody>().velocity = new Vector3(-4, 0, 3);
lanechange = "y";
StartCoroutine (stoplanechange());
}
if((Input.GetKey("d")) && (lanechange=="n") && (transform.position.x<.9f) )
{
GetComponent<Rigidbody>().velocity = new Vector3(4, 0, 3);
lanechange = "y";
StartCoroutine (stoplanechange());
}
if ((Input.GetKey("space")) && (jumpcontrol == "n") )
{
GetComponent<Rigidbody>().velocity = new Vector3(0, 2, 3);
jumpcontrol = "y";
StartCoroutine(stopjump());
}
}
IEnumerator stopjump()
{
yield return new WaitForSeconds(0.5f);
GetComponent<Rigidbody>().velocity = new Vector3(0, -2, 3);
yield return new WaitForSeconds(0.5f);
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 3);
jumpcontrol = "n";
}
IEnumerator stoplanechange()
{
yield return new WaitForSeconds(0.25f);
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 3);
lanechange = "n";
Debug.Log(GetComponent<Transform>().position);
}
私有字符串lanechange=“n”;
专用字符串jumpcontrol=“n”;
void Start()
{
GetComponent().velocity=新向量3(0,0,3);
}
无效更新()
{
if((Input.GetKey(“a”)&&&(lanechange==“n”)&&&(transform.position.x>-.9f))
{
GetComponent().velocity=新向量3(-4,0,3);
lanechange=“y”;
开始例行程序(stoplanechange());
}
如果((Input.GetKey(“d”)&&&(lanechange==“n”)&&(transform.position.x如果使用刚体和速度使其移动,则使用物理系统使角色移动。物理系统具有不同的时间步,该时间步链接到
FixedUpdate()
函数,不进行更新
但是输入总是绑定到Update()
,这与图形和键盘输入同步
所以,像这样的移动计时总是会有一点偏差,甚至取决于图形帧率
我建议你设置一些逻辑,使你的角色锁定在你最近的车道上。移动键使它大致移动,至少移动车道宽度的一半,然后回到中间来控制他并使他居中。如果你使用刚体和速度使其移动,你是使用物理系统使你的角色移动er move。物理系统有一个不同的时间步,它链接到
FixedUpdate()
函数,而不是更新
但是输入总是绑定到Update()
,这与图形和键盘输入同步
所以,像这样的移动计时总是会有一点偏差,甚至取决于图形帧率
我建议你设置一些逻辑,使你的角色锁定在你最近的车道上。移动键使它大致移动,至少移动车道宽度的一半,然后从后向中间移动并使他居中。是的。我更改了此移动的机制。谢谢。是的。我更改了此移动的机制。谢谢。