首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使全国人民代表大会上下统一

使全国人民代表大会上下统一
EN

Stack Overflow用户
提问于 2019-03-08 12:34:24
回答 3查看 183关注 0票数 0

所以我有这个代码,它是使用航点来制造一个敌人,它实际上是有效的,但是由于一个奇怪的原因,在航点之间循环之后,它一直到通过顶部航点的无限远(wp0),为什么会发生这种情况?我一直在摆弄代码,但没有得到适当的行为。

这段代码是为有运动学刚体的NPC编写的,它“漂浮”在空中,可以像马里奥兄弟游戏中飞来飞去的koopas一样上下飞行……

代码语言:javascript
运行
复制
public GameObject[] myWaypoints;
float direction = 1f; //1 up, 0 stop. -1 down.
int _myWaypointIndex = 0; // used as index for My_Waypoints
float _moveTime;
float _vx = 0f;
bool _moving = true;
public float waitAtWaypointTime = 1f;   // how long to wait at a waypoint
public bool loopWaypoints = true; // should it loop through the waypoints

void EnemyMovement()
{
    // if there isn't anything in My_Waypoints
    if ((myWaypoints.Length != 0) && (_moving))
    {

        // make sure the enemy is facing the waypoint (based on previous movement)
        Flip(_vx);

        // determine distance between waypoint and enemy
        _vx = myWaypoints[_myWaypointIndex].transform.position.y - _transform.position.y;

        direction = Mathf.Sign(myWaypoints[_myWaypointIndex].transform.position.y);
        Debug.Log("Direction = " + direction);

        // if the enemy is close enough to waypoint, make it's new target the next waypoint
        if (Mathf.Abs(_vx) <= 0.05f)
        {
            Debug.Log("changin to the next waypoint");

            // At waypoint so stop moving
            _rigidbody.velocity = new Vector2(0, 0);

            // increment to next index in array
            _myWaypointIndex++;

            Debug.Log("_myWaypointIndex = " + _myWaypointIndex);
            Debug.Log("Length = " + myWaypoints.Length);
            direction *= -1;
            Debug.Log("New Direction = " + direction);

            // reset waypoint back to 0 for looping
            if (_myWaypointIndex >= myWaypoints.Length)
            {
                Debug.Log("True");
                if (loopWaypoints)
                {
                    _myWaypointIndex = 0;
                }
                else
                {
                    _moving = false;
                }
            }

            // setup wait time at current waypoint
            _moveTime = Time.time + waitAtWaypointTime;
        }
        else
        {
            // enemy is moving
            _animator.SetBool("Moving", true);

            // Set the enemy's velocity to moveSpeed in the y direction.
            _rigidbody.velocity = new Vector2(0.0f, _transform.localScale.y * moveSpeed * direction);

        }

    }
}
EN

回答 3

Stack Overflow用户

发布于 2019-03-08 18:22:28

我认为问题是当你将_myWaypointIndex重置为0时,loopWaypoints在其他函数中,但是else不会使NPC停止,所以可能会继续移动到下一个无法找到的点,因为索引没有被重置,所以_vx距离永远不会为0,尝试检查loopWaypoints是否在你想要的时候改变了值,错误可能就在那里。

代码语言:javascript
运行
复制
        if (loopWaypoints)
        {
            _myWaypointIndex = 0;
        }
        else
        {
            _moving = false;
            //Stop the movment 
            moveSpeed = 0;
        }
票数 0
EN

Stack Overflow用户

发布于 2019-03-08 18:28:09

也许:如果敌人通过了最后一个航点和_vx> 0.05f会发生什么?

它不会被捕捉到:

代码语言:javascript
运行
复制
if (Mathf.Abs(_vx) <= 0.05f)
票数 0
EN

Stack Overflow用户

发布于 2019-03-08 19:01:27

问题是,在Mathf.abs( _vx ) <= 0.05f中,_vx值永远不会低于0.05f。

你应该做的是在航路点附加一个触发器,当玩家接触到该触发器时,更改航点坐标。

或者这样做。

代码语言:javascript
运行
复制
_vx =   Mathf.Abs(myWaypoints 
[_myWaypointIndex].  
transform.position.y) - 
Mathf.Abs(_transform.position.y);

//And then write.

if (_vx <= 0.5f)
   // change coordinate.'''
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55056779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档