Exemplo n.º 1
0
    void MoveToTarget()
    {
        Vector3 _acceleration = new Vector3(0.0f, 0.0f, 0.0f);

        var targetlist   = _trainData.ListTurret;
        int targetSize   = targetlist.Length;
        int randomtarget = Random.Range(0, targetSize);

        targetPos = targetlist[randomtarget].gameObject.transform.position;

        Vector2 destination = new Vector2(targetPos.x, transform.position.y);

        _acceleration = BehaviourUpdate.BehaviourUpdated(SeekBehaviour.SeekMove(transform, destination, enemyData.MaxSpeed), enemyData.SeekWeight);
        _velocity    += _acceleration * Time.deltaTime;
        float dis = float.MinValue;

        if (Mathf.Abs(transform.position.x - destination.x) < 0.1f)
        {
            mCurrentState   = State.Charging;
            _nextAttackTime = Time.time + enemyData.AttackDelay + Random.Range(-enemyData.AttackDelay * 0.1f, enemyData.AttackDelay * 0.1f);
        }
        else
        {
            transform.position += _velocity * Time.deltaTime;
        }
    }
Exemplo n.º 2
0
    public static Vector2 WanderMove(Transform agent, float mWanderRadius, float mWanderDistance, float mWanderJitter, float speed)
    {
        Vector2 mWanderPoint = Vector2.zero;

        mWanderPoint = mWanderPoint + Random.insideUnitCircle * mWanderJitter;
        mWanderPoint = mWanderPoint.normalized * mWanderRadius;
        //wanderPoint.y += wanderDistance;
        var     wanderTarget = mWanderPoint + new Vector2(0.0f, mWanderDistance);
        var     localToWorld = agent.transform.localToWorldMatrix;
        Vector2 destination  = localToWorld.MultiplyVector((Vector3)wanderTarget);

        mWanderPoint = SeekBehaviour.SeekMove(agent, destination, speed);
        //mRenderPoint = destination;
        return(mWanderPoint);
    }
    public void GroupBehaviours()
    {
        Vector3 _acceleration = new Vector3(0.0f, 0.0f, 0.0f);


        foreach (var agent in swarmNeighbors)
        {
            if (!agent.Attacking)
            {
                _acceleration   = BehaviourUpdate.BehaviourUpdated(WanderBehavior.WanderMove(agent.transform, enemyData.WanderRadius, enemyData.WanderDistance, enemyData.WanderJitter, 1.0f), enemyData.WanderBehaviorWeight);
                _acceleration  += (Vector3)BehaviourUpdate.BehaviourUpdated(CohesionBehavior.CalculateMove(agent.transform, swarmNeighborsTrans), enemyData.CohesionBehaviorWeight);
                _acceleration  += (Vector3)BehaviourUpdate.BehaviourUpdated(SeparationBehavior.SeparationMove(agent.transform, swarmNeighborsTrans, enemyData.SeparationBehaviorRadius), enemyData.SeparationBehaviorWeight);
                _acceleration  += (Vector3)BehaviourUpdate.BehaviourUpdated(AlignmentBehavior.CalculateMove(agent.transform, swarmNeighborsTrans), enemyData.AlignmentBehaviorWeight);
                agent.Velocity += _acceleration * Time.deltaTime;
                if (agent.Velocity.magnitude > enemyData.MaxSpeed)
                {
                    agent.Velocity.Normalize();
                    agent.Velocity *= enemyData.MaxSpeed;
                }


                if (agent.transform.position.x < _bottomLeft.position.x)
                {
                    agent.Velocity *= -1;
                }
                if (agent.transform.position.x > _topright.position.x)
                {
                    agent.Velocity *= -1;
                }
                if (agent.transform.position.y < _topright.position.y)
                {
                    agent.Velocity *= -1;
                }
                if (agent.transform.position.y > _bottomLeft.position.y)
                {
                    agent.Velocity *= -1;
                }
                //agent.transform.position += agent.Velocity * Time.deltaTime;
            }
            else
            {
                _acceleration   = BehaviourUpdate.BehaviourUpdated(SeekBehaviour.SeekMove(agent.transform, agent.Target, enemyData.Swarm_AttackSpeed), enemyData.SeekBehaviorWeight);
                agent.Velocity += _acceleration * Time.deltaTime;
            }
            Vector3 deltaPos = agent.Velocity * Time.deltaTime;
            if (deltaPos.x == 0 && deltaPos.y == 0)
            {
                Debug.Log("Enemy is stuck!");
            }

            agent.transform.position += agent.Velocity * Time.deltaTime;
            //_acceleration += (Vector3)(BehaviourUpdate.BehaviourUpdated(WallAvoidance.WallAvoidanceCalculation(agent.transform, _bottomLeft.position.x, _topright.position.x, _topright.position.y, _bottomLeft.position.y), enemyData.WallAvoidWeight));
        }
        //transform.position += _velocity * Time.deltaTime;
        //        {
        //            move = BehaviourUpdate.BehaviourUpdated(CohesionBehavior.CalculateMove(agent.transform, swarmNeighbors), 5.0f);
        //            move *= speed;
        //            if (move.sqrMagnitude > squareMaxSpeed)
        //            {
        //                move = move.normalized * maxSpeed;
        //            }
        //            agent.transform.position += (Vector3)move * Time.deltaTime;
        //        }
    }