2016-08-15 57 views
0

我想拥有一个GameObject,它跟随我的玩家并始终停留在他的左边。 我不想让它成为一个孩子,因为我看起来不自然,我也玩过NavmeshAgent,但第二个对象太慢/移动太多。让一个GameObject成为一名队员

感谢您的任何建议!

+0

'NavmeshAgent'是一个很好的解决方案。你试过调整'speed','angularSpeed','acceleration'? –

+0

是的,我玩过它,它通常太慢或物体加速如此之快,以致它爆发并像钟摆一样。也许我不明白这三个值如何在一起。 – kapuetze

回答

0

一种解决方案是这样的:

  1. 添加一个空的游戏物体(我称之为SquadPoint)作为主要选手对象的孩子,并将其放置在所需位置(左手)。

  2. 地方实际的小队成员直接在现场(我假设小队成员具有连接到它,我把它叫做SquadMemberController控制器脚本)

  3. 下面的代码添加到SquadMemberController脚本

这应该使小队成员跟随SquadPoint,并且由于SquadPoint是主要球员的小孩,它会随主球员移动和旋转。

const float threshold = 1f; 

public float speed;   //set in inspector 
public Transform SquadPoint; //set in inspector 

void FixedUpdate(){ 

    //move towards SquadPoint with given speed 
    var distance = SquadPoint.position - transform.position; 
    if(distance.magnitude > threshold) 
    { 
     var direction = distance.normalized; 
     transform.position += direction * speed * time.fixedDeltaTime; 

     //rotate immediately 
     transform.LookAt(SquadPoint.position); 
    } 
    else 
    { 
     //... very close to main player 
    } 
} 
+0

我调整了旋转一点,使其适合我的,但它是一个很好的解决方案。非常感谢你! – kapuetze

相关问题