gpt4 book ai didi

unity3d - 如何以某种方式移动角色?

转载 作者:行者123 更新时间:2023-12-01 12:11:27 25 4
gpt4 key购买 nike

有办法。角色从中点开始。然后它应该向右移动,如果 HorizontalAxis > 0在左边,如果 < 0 .我做过这样的事情-

方式:

way image

public Transform[] PatrolPoints;
public Transform Way;
public int CurrentPoint;
public int NeedPoint;

if (transform.position == PatrolPoints[CurrentPoint].position && CurrentPoint < PatrolPoints.Length-1)
{
//PatrolPoints[CurrentPoint];
NeedPoint = CurrentPoint + 1;
}
else if(transform.position == PatrolPoints[NeedPoint].position && CurrentPoint != PatrolPoints.Length - 1)
{
CurrentPoint++;
}
else if(transform.position == PatrolPoints[PatrolPoints.Length - 1].position)
{
NeedPoint--;
}
if (CurrentPoint != PatrolPoints.Length - 1 && moveAxis > 0)
{
transform.position = Vector3.MoveTowards(transform.position, PatrolPoints[NeedPoint].position, moveAxis * moveRate * Time.deltaTime);
//An error occurs here
}
else if(CurrentPoint > 0 && moveAxis < 0)
{
transform.position = Vector3.MoveTowards(transform.position, PatrolPoints[NeedPoint - 1].position, -moveAxis * moveRate * Time.deltaTime);
}

数组的长度 - 3 个元素。从中间点向右,角色通常会向左和向右移动,但是当我到达最后一个点时,出现错误 -

Array index is out of range

期间出错的变量有:

需要点:-1;

当前点:2;

而且从中间点到左边点,角色根本没有移动。

最佳答案

这是解决方案:

The character starts at the midpoint. Then it should move to the right, if HorizontalAxis > 0 and to the left, if < 0.

我假设“向右移动”意味着增加数组中的索引,“向左移动”意味着减少数组中的索引

public Transform[] PatrolPoints;
public int NextPointOnLeft = 0; // because player starts at 1
public int NextPointOnRight = 2; // because player starts at 1

if (HorizontalAxis < 0)
{
if (transform.position == PatrolPoints[NextPointOnLeft].position)
{
if(NextPointOnLeft > 0)
{
--NextPointOnLeft;
}
// EDIT: NextPointOnRight = NextPointOnLeft + 1;
}
// EDIT:
NextPointOnRight = NextPointOnLeft + 1;
transform.position = Vector3.MoveTowards(transform.position, PatrolPoints[NextPointOnLeft].position, moveRate * Time.deltaTime);
}
if (HorizontalAxis > 0)
{
if (transform.position == PatrolPoints[NextPointOnRight].position)
{
if(NextPointOnRight < PatrolPoints.Length-1)
{
++NextPointOnRight;
}
// EDIT: NextPointOnLeft = NextPointOnRight - 1;
}
// EDIT:
NextPointOnLeft = NextPointOnRight - 1;
transform.position = Vector3.MoveTowards(transform.position, PatrolPoints[NextPointOnRight].position, moveRate * Time.deltaTime);
}

如果您需要不同的解决方案,请说明您需要什么

关于unity3d - 如何以某种方式移动角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51484416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com