我有一个角色每两秒换一次脸(右或左)。在那两秒之后,速度乘以-1,所以它改变了方向,但是它一直向右(->)。
这是我的密码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour {
public int speed = 2;
void Start ()
{
StartCoroutine(Animate ());
}
void Update ()
{
float auto = Time.deltaTime * speed;
transform.Translate (auto, 0, 0);
}
IEnumerator Animate()
{
while (true) {
yield return new WaitForSeconds (2);
transform.rotation = Quaternion.LookRotation (Vector3.back);
speed *= -1;
yield return new WaitForSeconds (2);
transform.rotation = Quaternion.LookRotation (Vector3.forward);
speed *= -1;
}
}
}
发布于 2017-01-04 15:33:04
这是因为transform.Translate
将物体翻译成局部空间,而不是世界空间。
当您执行以下操作时:
// The object will look at the opposite direction after this line
transform.rotation = Quaternion.LookRotation (Vector3.back);
speed *= -1;
你翻转你的对象和,你要求朝相反的方向走。因此,对象将在随后的初始方向上进行转换。
要解决问题,我建议您不要更改speed
变量的值。
试着想象自己在同样的情况下:
最后,你“继续”你的道路在同一方向。
以下是最后一种方法:
IEnumerator Animate()
{
WaitForSeconds delay = new WaitForSeconds(2) ;
Quaterion backRotation = Quaternion.LookRotation (Vector3.back) ;
Quaterion forwardRotation = Quaternion.LookRotation (Vector3.forward) ;
while (true)
{
yield return delay;
transform.rotation = backRotation;
yield return delay;
transform.rotation = forwardRotation;
}
}
https://stackoverflow.com/questions/41467459
复制相似问题