猿问

移动游戏对象

我正在尝试使用刚体创建 MOB AI。我想让暴徒(GameObject)使用

mobrigid.AddForce((((goal - transform.position).normalized)*speed * Time.deltaTime));

(目标是暴民周围的一个随机地方)。

这就是它变得复杂的地方,一些暴徒以极快的速度在空中飞行,而另一些则以低速正常移动。(是的,我确实确保目标。Y 是在地面上而不是空中)。我尝试通过改变阻力来修复它,但这会导致暴徒在空中行走而不会因重力而下落。

我真的很迷茫,不知道如何简单地用刚体移动游戏对象而不会出现这种奇怪的行为。

怪物日志(目标 Y 更改为 0): 

编辑:
暴徒的形象:

http://img3.mukewang.com/61752c040001030b02010379.jpg

我的运动逻辑:


   case MOBTYPE.EARTHMOB:

            switch (currentstatus)

            {

                case MOBSTATUS.STANDING:


                        if (mobrigid.IsSleeping())

                        {

                            mobrigid.WakeUp();

                        }   




                        goal = this.transform.position;

                            goal.x = Random.Range(goal.x - 150, goal.x + 150);

                            goal.z = Random.Range(goal.z -150, goal.z + 150);

                    goal.y = 0;


                                    currentstatus = MOBSTATUS.WALKING;


                 //   Debug.Log("Transform:"+this.transform.position+ "Goal:" + goal+ "goal-transform:" + (goal - transform.position));


                    break;

                case MOBSTATUS.WALKING:

                    if (Random.Range(1, 100) == 5)

                    {

                        currentstatus = MOBSTATUS.STANDING;

                    }

                    if (mobrigid.IsSleeping())

                    {

                        mobrigid.WakeUp();

                    }

                    mobrigid.AddForce((((goal - transform.position).normalized) * 10000 * Time.deltaTime));

                    // transform.LookAt(goal);

                    var distance = Vector3.Distance(goal, gameObject.transform.position);

                    if (distance <=5)

                    {


                        currentstatus = MOBSTATUS.STANDING;


                    }


                    break;

            }

            break;

地形图:

http://img4.mukewang.com/61752c14000159b309200539.jpg

红糖糍粑
浏览 157回答 1
1回答

叮当猫咪

该Rigidbody.AddForce函数用于沿某个方向向对象添加力。由于您有一个需要移动Rigidbody到的位置,因此您必须使用该Rigidbody.MovePosition功能。您可能还想将刚体标记为运动学。将 Rigidbody 对象移动到特定位置的简单协程函数:IEnumerator MoveRigidbody(Rigidbody rb, Vector3 destination, float speed = 50f){&nbsp; &nbsp; const float destThreshold = 0.4f;&nbsp; &nbsp; while (true)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Vector3 direction = (destination - rb.position).normalized;&nbsp; &nbsp; &nbsp; &nbsp; rb.MovePosition(rb.position + direction * speed * Time.deltaTime);&nbsp; &nbsp; &nbsp; &nbsp; float dist = Vector3.Distance(rb.position, destination);&nbsp; &nbsp; &nbsp; &nbsp; //Exit function if we are very close to the destination&nbsp; &nbsp; &nbsp; &nbsp; if (dist <= destThreshold)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield break;&nbsp; &nbsp; &nbsp; &nbsp; yield return null;&nbsp; &nbsp; &nbsp; &nbsp; //yield return new WaitForFixedUpdate();&nbsp; &nbsp; }}最好从另一个 coorutine 函数调用它,以便您可以让步或等待它完成,然后执行其他任务,例如再次生成随机位置并将刚体移动到那里。您想生成新位置然后将其移动到Rigidbody那里,这是如何调用上述函数的示例:IEnumerator StartMoveMent(){&nbsp; &nbsp; Rigidbody targetRb = GetComponent<Rigidbody>();&nbsp; &nbsp; while (true)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //Generate random position&nbsp; &nbsp; &nbsp; &nbsp; Vector3 destination = new Vector3();&nbsp; &nbsp; &nbsp; &nbsp; destination.y = 0;&nbsp; &nbsp; &nbsp; &nbsp; destination.x = UnityEngine.Random.Range(0, 50);&nbsp; &nbsp; &nbsp; &nbsp; destination.z = UnityEngine.Random.Range(0, 50);&nbsp; &nbsp; &nbsp; &nbsp; //Move and wait until the movement is done&nbsp; &nbsp; &nbsp; &nbsp; yield return StartCoroutine(MoveRigidbody(targetRb, destination, 30f));&nbsp; &nbsp; }}并启动该StartMoveMent功能:void Start(){&nbsp; &nbsp; StartCoroutine(StartMoveMent());}虽然我上面所说的应该可行,但我建议您使用 Unity 的内置寻路系统。这是一个教程。它简化了寻找通往目的地的路径,NavMesh也可以使用baked during run-time.
随时随地看视频慕课网APP
我要回答