Jho*_*per 5 c# unity-game-engine
首先,对不起,这不是写得很好,我花了几个小时调试这个,我很紧张。我正在尝试制作一个可以在路径点之间移动的统一移动平台,但我不想让世界上有大量游戏对象占用宝贵的处理能力,所以我正在尝试使用我可以使用的东西只需通过编辑器添加到脚本中即可。
唯一的问题是它似乎以令人难以置信的速度执行此操作:
黑色 = 摄像机视图,蓝色 = 平台以及基于航路点的位置,红色 = 当前正在做什么。
我花了几个小时试图找到解决办法,但我不知道为什么要这样做。
我在平台上的脚本:
public Vector3[] localWaypoints;
Vector3[] globalWaypoints;
public float speed;
public bool cyclic;
public float waitTime;
[Range(0, 2)]
public float easeAmount;
int fromWaypointIndex;
float percentBetweenWaypoints;
float nextMoveTime;
void Start()
{
globalWaypoints = new Vector3[localWaypoints.Length];
for (int i = 0; i < localWaypoints.Length; i++)
{
globalWaypoints[i] = localWaypoints[i] + transform.position;
}
}
void Update()
{
Vector3 velocity = CalculatePlatformMovement();
transform.Translate(velocity);
}
float Ease(float x)
{
float a = easeAmount + 1;
return Mathf.Pow(x, a) / (Mathf.Pow(x, a) + Mathf.Pow(1 - x, a));
}
Vector3 CalculatePlatformMovement()
{
if (Time.time < nextMoveTime)
{
return Vector3.zero;
}
fromWaypointIndex %= globalWaypoints.Length;
int toWaypointIndex = (fromWaypointIndex + 1) % globalWaypoints.Length;
float distanceBetweenWaypoints = Vector3.Distance(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex]);
percentBetweenWaypoints += Time.deltaTime * speed / distanceBetweenWaypoints;
percentBetweenWaypoints = Mathf.Clamp01(percentBetweenWaypoints);
float easedPercentBetweenWaypoints = Ease(percentBetweenWaypoints);
Vector3 newPos = Vector3.Lerp(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex], easedPercentBetweenWaypoints);
if (percentBetweenWaypoints >= 1)
{
percentBetweenWaypoints = 0;
fromWaypointIndex++;
if (!cyclic)
{
if (fromWaypointIndex >= globalWaypoints.Length - 1)
{
fromWaypointIndex = 0;
System.Array.Reverse(globalWaypoints);
}
}
nextMoveTime = Time.time + waitTime;
}
return newPos - transform.position;
}
struct PassengerMovement
{
public Transform transform;
public Vector3 velocity;
public bool standingOnPlatform;
public bool moveBeforePlatform;
public PassengerMovement(Transform _transform, Vector3 _velocity, bool _standingOnPlatform, bool _moveBeforePlatform)
{
transform = _transform;
velocity = _velocity;
standingOnPlatform = _standingOnPlatform;
moveBeforePlatform = _moveBeforePlatform;
}
}
void OnDrawGizmos()
{
if (localWaypoints != null)
{
Gizmos.color = Color.red;
float size = .3f;
for (int i = 0; i < localWaypoints.Length; i++)
{
Vector3 globalWaypointPos = (Application.isPlaying) ? globalWaypoints[i] : localWaypoints[i] + transform.position;
Gizmos.DrawLine(globalWaypointPos - Vector3.up * size, globalWaypointPos + Vector3.up * size);
Gizmos.DrawLine(globalWaypointPos - Vector3.left * size, globalWaypointPos + Vector3.left * size);
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:经过进一步测试,我发现如果我的 localWaypoint 数组中的第一个对象设置为 0,0,0 并且我的第二个对象设置为 1,0,0 那么平台将向右螺旋,确保击中航路点,因为它螺旋上升,然后螺旋上升到与上图不同的地方。但是,如果我将第一个对象设置为 0,0,0,将第二个对象设置为 -1,0,0,则该对象的行为方式与之前相同,但会像此图像中显示的那样向左螺旋。(第二张图片也进行了更新,以显示平台如何确保在螺旋上升到无处之前击中两个航路点)。
我还注意到,如果我将两个航点都设置为 0,0,0 那么平台会保持静止,这两件事证明它与航点的处理方式有关,而不是其他一些脚本或父对象的干扰.
使用更新后的数字 ([0,0,0], [1,0,0]) 在我的测试应用程序中有效。但是,如果我在对象的 Y 轴上进行旋转,那么我会看到像您所看到的行为。在更新中,如果您更改:
transform.Translate(velocity);
Run Code Online (Sandbox Code Playgroud)
到
transform.Translate(velocity, Space.World);
Run Code Online (Sandbox Code Playgroud)
您应该看到您想要的行为。请注意,“transform.Translate(velocity)”与“transform.Translate(velocity, Space.Self)”相同。您的翻译正在轮换。
如果您好奇,请查看此内容以获取有关如何应用转换中的值的更多信息: https://gamedev.stackexchange.com/questions/138358/what-is-the-transformation-order-when-using - 变换类