0

ここにあるボイドのフロックスクリプトを変換しようとしています: http://wiki.unity3d.com/index.php?title=Flocking

私の 2D ゲームでは動作しますが、群れはターゲットにまったく追従しません。群れは、約 20 度の角度で右から上に移動したいだけのようです。このスクリプトで群れにターゲットまたは「Chasee」を追跡させるにはどうすればよいですか?

簡単にするために、ほとんどの座標を Vector3 のままにして、速度を調整するときにそれらを Vector 2 にキャストします。

using UnityEngine;
using System.Collections;

public class BoidController : MonoBehaviour
{
    public float minVelocity = 5;
    public float maxVelocity = 20;
    public float randomness = 1;
    public int flockSize = 20;
    public GameObject prefab;
    public GameObject chasee;

    public Vector3 flockCenter;
    public Vector3 flockVelocity;

    private GameObject[] boids;

    void Start()
    {
        boids = new GameObject[flockSize];
        for (var i=0; i<flockSize; i++)
        {
            Vector3 position = new Vector3 (
                Random.value *  GetComponent<BoxCollider2D>().bounds.size.x,
                Random.value *  GetComponent<BoxCollider2D>().bounds.size.y
                ) -  GetComponent<BoxCollider2D>().bounds.extents;

            GameObject boid = Instantiate(prefab, transform.position, transform.rotation) as GameObject;
            boid.transform.parent = transform;
            boid.transform.localPosition = position;
            boid.GetComponent<BoidFlocking>().SetController (gameObject);
            boids[i] = boid;
        }
    }

    void Update ()
    {
        Vector3 theCenter = Vector3.zero;
        Vector3 theVelocity = Vector3.zero;

        foreach (GameObject boid in boids)
        {
            theCenter = theCenter + boid.transform.localPosition;
            theVelocity = theVelocity + (Vector3) boid.GetComponent<Rigidbody2D>().velocity;
        }

        flockCenter = theCenter/(flockSize);
        flockVelocity = theVelocity/(flockSize);
    }
}

そして、群がる行動

using UnityEngine;
using UnityEngine;
using System.Collections;

public class BoidFlocking : MonoBehaviour
{
    private GameObject Controller;
    private bool inited = false;
    private float minVelocity;
    private float maxVelocity;
    private float randomness;
    private GameObject chasee;

    void Start ()
    {
        StartCoroutine ("BoidSteering");
    }

    IEnumerator BoidSteering()
    {
        while (true)
        {
            if (inited)
            {
                GetComponent<Rigidbody2D>().velocity = GetComponent<Rigidbody2D>().velocity + (Vector2) Calc () * Time.deltaTime;

                // enforce minimum and maximum speeds for the boids
                float speed = GetComponent<Rigidbody2D>().velocity.magnitude;
                if (speed > maxVelocity)
                {
                    GetComponent<Rigidbody2D>().velocity = GetComponent<Rigidbody2D>().velocity.normalized * maxVelocity;
                }
                else if (speed < minVelocity)
                {
                    GetComponent<Rigidbody2D>().velocity = GetComponent<Rigidbody2D>().velocity.normalized * minVelocity;
                }
            }

            float waitTime = Random.Range(0.3f, 0.5f);
            yield return new WaitForSeconds (waitTime);
        }
    }

    private Vector3 Calc()
    {
        Vector3 randomize = new Vector3 ((Random.value *2) -1, (Random.value * 2) -1, (Random.value * 2) -1);

        randomize.Normalize();
        BoidController boidController = Controller.GetComponent<BoidController>();
        Vector3 flockCenter = boidController.flockCenter;
        Vector3 flockVelocity = boidController.flockVelocity;
        Vector3 follow = chasee.transform.localPosition;

        flockCenter = flockCenter - transform.localPosition;
        flockVelocity = flockVelocity - (Vector3) GetComponent<Rigidbody2D>().velocity;
        follow = follow - transform.localPosition;

        return (flockCenter + flockVelocity + follow * 2 + randomize * randomness);
    }

    public void SetController (GameObject theController)
    {
        Controller = theController;
        BoidController boidController = Controller.GetComponent<BoidController>();
        minVelocity = boidController.minVelocity;
        maxVelocity = boidController.maxVelocity;
        randomness = boidController.randomness;
        chasee = boidController.chasee;
        inited = true;
    }
}
4

1 に答える 1

0

フォロー方向を計算するときにチェイスとボイドでローカル位置を使用していましたが、それらは異なるオブジェクトの子です。になるはずだった

Vector3 follow = chasee.transform.position;
follow = follow - transform.position;
于 2015-09-19T12:08:08.627 に答える