示例#1
0
    public void Start()
    {
        for (int i = 0; i < unitAmount; ++i)
        {
            GameObject newUnitGO = GameObject.Instantiate(unitTemplate);
            newUnitGO.SetActive(true);
            JobTestUnit newUnit = newUnitGO.GetComponent <JobTestUnit> ();
            units.Add(newUnit);
        }

        positionsArray    = new NativeArray <float2> (units.Count, Allocator.Persistent);
        newPositionsArray = new NativeArray <float2> (units.Count, Allocator.Persistent);
        directionArray    = new NativeArray <float2> (units.Count, Allocator.Persistent);
    }
示例#2
0
    public void FixedUpdate()
    {
        if (!useJobs)
        {
            for (int i = 0; i < units.Count; ++i)
            {
                CollisionDetection(i);
            }
        }
        else
        {
            //NativeArray<float2> positionsArray = new NativeArray<float2> ( units.Count, Allocator.TempJob );
            //NativeArray<float2> newPositionsArray = new NativeArray<float2> ( units.Count, Allocator.TempJob );
            //TransformAccessArray transformAccessArray = new TransformAccessArray ( units.Count );

            for (int i = 0; i < units.Count; ++i)
            {
                JobTestUnit jobTestUnit = units [i];
                positionsArray [i]    = jobTestUnit.position;
                newPositionsArray [i] = Vector2.zero;
                directionArray [i]    = jobTestUnit.randomPosition;
                //transformAccessArray.Add ( units [ i ].body );
            }

            JobCollisionTest2 jobCollisionTest = new JobCollisionTest2 {
                units      = positionsArray,
                positions  = newPositionsArray,
                directions = directionArray,
                unitSize   = units [0].size,
                offset     = units [0].offset,
                random     = new Unity.Mathematics.Random((uint)UnityEngine.Random.Range(0, 10000000))
            };

            JobHandle jobHandle = jobCollisionTest.Schedule(units.Count, 100);
            jobHandle.Complete();

            for (int i = 0; i < units.Count; ++i)
            {
                float2 pos = newPositionsArray [i];
                units [i].body.position = units [i].body.position + new Vector3(pos.x, pos.y);
                //units [ i ].body.position = new Vector3 ( pos.x, pos.y );
            }

            //positionsArray.Dispose ();
            //newPositionsArray.Dispose ();
        }
    }
示例#3
0
    public void CollisionDetection(int i)
    {
        JobTestUnit jobTestUnit = units [i];
        Vector2     position = jobTestUnit.position;
        Vector2     tempVector = Vector2.zero, tempVector2 = Vector2.zero;

        for (int a = 0; a < units.Count; ++a)
        {
            if (a == i)
            {
                continue;
            }

            JobTestUnit otherUnit     = units [a];
            Vector2     otherPosition = otherUnit.position;

            if (!(position.x < otherPosition.x + otherUnit.size.x && position.x + jobTestUnit.size.x > otherPosition.x &&
                  position.y + jobTestUnit.offset.y < otherPosition.y + jobTestUnit.offset.y + otherUnit.size.y &&
                  position.y + jobTestUnit.offset.y + jobTestUnit.size.y > otherPosition.y + jobTestUnit.offset.y))
            {
                continue;
            }

            float x            = otherUnit.position.x - position.x + UnityEngine.Random.Range(-0.00001f, 0.00001f);
            float y            = (otherPosition.y + jobTestUnit.size.y) - (position.y + jobTestUnit.size.y) + UnityEngine.Random.Range(-0.00001f, 0.00001f);
            float distance     = Mathf.Sqrt(x * x + y * y);
            float maxDistanceX = jobTestUnit.size.x;
            float maxDistanceY = jobTestUnit.size.y;
            tempVector2.x             = x / distance;
            tempVector2.y             = y / distance;
            tempVector                = position;
            tempVector.x             -= tempVector2.x * Mathf.Abs(x < 0 ? -maxDistanceX - x : maxDistanceX - x);
            tempVector.y             -= tempVector2.y * Mathf.Abs(y < 0 ? -maxDistanceY - y : maxDistanceY - y);
            jobTestUnit.body.position = tempVector;
            position = jobTestUnit.position;
        }
    }