我有一个移动平台脚本,可以在三个位置之间移动。这很好。
我有一个将播放器保存到平台的脚本,这也很好。
但是当我通过移动平台脚本将我的播放器固定到一个对象的平台时,播放器的移动是不稳定且不均匀的。
以下是我正在使用的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingMultipalPoint : MonoBehaviour
{
public Vector3 pointB;
public Vector3 pointC;
public float Second = 5.0f;
public float Speed = 3.0f;
//public float HoldSecond = 5.0f;
IEnumerator Start()
{
Vector3 pointA = transform.position;
while (true)
{
yield return new WaitForSeconds(Second);
yield return StartCoroutine(MoveObject(transform, pointA, pointB, Speed));
yield return new WaitForSeconds(Second);
yield return StartCoroutine(MoveObject(transform, pointB, pointC, Speed));
yield return new WaitForSeconds(Second);
yield return StartCoroutine(MoveObject(transform, pointC, pointB, Speed));
yield return new WaitForSeconds(Second);
yield return StartCoroutine(MoveObject(transform, pointB, pointA, Speed));
}
}
IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
float i = 0.0f;
float rate = 1.0f / time;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
//newRotation *= Quaternion.Euler(0, 90, 0);
//transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, 20 * Time.deltaTime);
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
}
}
}
此脚本将播放器保存到平台。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HoldPlayer : MonoBehaviour
{
private GameObject target = null;
private Vector3 offset;
void Start()
{
target = null;
}
void OnTriggerStay(Collider col)
{
target = col.gameObject;
offset = target.transform.position - transform.position;
}
void OnTriggerExit(Collider col)
{
target = null;
}
void LateUpdate()
{
if (target != null)
{
target.transform.position = transform.position + offset;
}
}
}
(第1张图)以下是在Inspector中配置移动平台的方法:
(第2张图片)和保存脚本:
Also here recorded video of issue
在这种情况下我是如何保持运动顺畅的?