我正在使用下面的代码附加到带有脊体和碰撞的圆柱体对象上。这个对象被放置在形成一个圆圈的碰撞障碍的中间。我试图让这个物体平稳地向前移动一段随机距离,在它移动后,等待一段随机时间然后再次移动。如果它撞到带有标签名称“墙”的障碍物,然后转身离开墙壁一定程度,以便它可以继续在里面移动。并继续执行以下代码。
然而,物体并没有平稳地移动,它几乎没有移动。
我怎样才能解决这个问题?提前感谢任何链接或帮助解决这个问题。
public bool hit, m = false;
public float waitTime;
public float movDistance;
public float fRotation;
public float transitionSpeed = 1f;
private float lerpTime = 3f;
private float currentLerpTime = 0f;
private Vector3 startPos, endPos;
void Start()
{
//rb = GetComponent<Rigidbody>();
startPos = this.transform.position;
}
private void Update()
{
if (m == false) {
if (hit == true) {
currentLerpTime += Time.deltaTime;
if (currentLerpTime >= lerpTime) {
currentLerpTime = lerpTime;
hit = false;
}
float p = currentLerpTime / lerpTime;
this.transform.position = Vector3.Lerp(startPos, endPos, p);
}
if (hit == false) {
setRanges();
m = true;
StartCoroutine(Example());
}
}
}
public void setRanges()
{
waitTime = Random.Range(4f, 10f);
movDistance = Random.Range(2f, 10f);
fRotation = Random.Range(1f, 270f);
endPos = this.transform.position + Vector3.forward * movDistance;
}
IEnumerator Example()
{
Debug.Log("Wait" + waitTime + "Seconds");
startPos = this.transform.position;
yield return new WaitForSeconds(waitTime);
hit = true;
m = false;
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "wall")
{
Debug.Log(" hit wall");
this.transform.rotation = Quaternion.Euler(0, 180, 0);
hit = false;
}
}