Unity预制机芯

时间:2018-06-21 19:32:17

标签: c# unity3d

您好,我想在游戏中跟随玩家创建一组相同的预制件。我已经掌握了可模仿播放器的预制组件,但是当播放器不止一个时,它们只是沿着完全相同的路径相互叠加。有没有办法让他们跟随玩家,却像一群蜜蜂在移动? 谢谢!

这是我的预制脚本:

USING PERIODIC COMMIT
load csv with headers from "file:///train.csv" as line
with line LIMIT 1458644
create   (pl:pickup_location{latitude:toFloat(line.pickup_latitude),longitude:toFloat(line.pickup_longitude)}),(pt:pickup_time{pickup:line.pickup_datetime}),(dl:dropoff_location{latitude:toFloat(line.dropoff_latitude),longitude:toFloat(line.dropoff_longitude)}),(dt:dropoff_time{dropoff:line.dropoff_datetime})
create (pl)-[:TLR]->(pt),(dl)-[:TLR]->(dt),(pl)-[:Trip]->(dl);

1 个答案:

答案 0 :(得分:0)

最佳解决方案取决于您的游戏逻辑,但是您可能会考虑在开始跟随之前有一个延迟(您可以根据特定对象在步道中所处的位置来调整延迟。

using System.Collections;
using UnityEngine;

public class Follower : MonoBehaviour
{
  public GameObject player;
  public float delay = 0f;
  public float speed = .5f;
  bool isReady = false;

  void Start()
  {
    StartFollowing();
  }

  public void StartFollowing()
  {
    StartCoroutine(WaitThenFollow(delay));
  }

  IEnumerator WaitThenFollow(float delay)
  {
    yield return new WaitForSeconds(delay);
    isReady = true;
    Debug.Log(gameObject.name);
    Debug.Log(Time.time);
  }

  void FixedUpdate()
  {
    if (isReady && player != null)
    {
      Vector2 toTarget = player.transform.position - transform.position;
      transform.Translate(toTarget * speed * Time.deltaTime);
    }
  }
}

我在Start方法中调用了StartFollowing,以供您测试代码。只要您的游戏逻辑合适,就应该调用此方法。