Example #1
0
    public void MoveAgent(float[] act)
    {
        shoot = false;

        if (Time.time > frozenTime + 4f && frozen)
        {
            Unfreeze();
        }
        if (Time.time > effectTime + 0.5f)
        {
            if (poisioned)
            {
                Unpoison();
            }
            if (satiated)
            {
                Unsatiate();
            }
        }

        Vector3 dirToGo   = Vector3.zero;
        Vector3 rotateDir = Vector3.zero;

        if (!frozen)
        {
            bool shootCommand = false;
            if (brain.brainParameters.vectorActionSpaceType == SpaceType.continuous)
            {
                dirToGo      = transform.forward * Mathf.Clamp(act[0], -1f, 1f);
                rotateDir    = transform.up * Mathf.Clamp(act[1], -1f, 1f);
                shootCommand = Mathf.Clamp(act[2], -1f, 1f) > 0.5f;
            }
            else
            {
                var forwardAxis = (int)act[0];
                var rightAxis   = (int)act[1];
                var rotateAxis  = (int)act[2];
                var shootAxis   = (int)act[3];

                switch (forwardAxis)
                {
                case 1:
                    dirToGo = transform.forward;
                    break;

                case 2:
                    dirToGo = -transform.forward;
                    break;
                }

                switch (rightAxis)
                {
                case 1:
                    dirToGo = transform.right;
                    break;

                case 2:
                    dirToGo = -transform.right;
                    break;
                }

                switch (rotateAxis)
                {
                case 1:
                    rotateDir = -transform.up;
                    break;

                case 2:
                    rotateDir = transform.up;
                    break;
                }
                switch (shootAxis)
                {
                case 1:
                    shootCommand = true;
                    break;
                }
            }
            if (shootCommand)
            {
                shoot             = true;
                dirToGo          *= 0.5f;
                agentRb.velocity *= 0.75f;
            }
            agentRb.AddForce(dirToGo * moveSpeed, ForceMode.VelocityChange);
            transform.Rotate(rotateDir, Time.fixedDeltaTime * turnSpeed);
        }

        if (agentRb.velocity.sqrMagnitude > 25f) // slow it down
        {
            agentRb.velocity *= 0.95f;
        }

        if (shoot)
        {
            myLaser.transform.localScale = new Vector3(1f, 1f, laser_length);
            Vector3 position = transform.TransformDirection(RayPerception3D.PolarToCartesian(25f, 90f));
            Debug.DrawRay(transform.position, position, Color.red, 0f, true);
            RaycastHit hit;
            if (Physics.SphereCast(transform.position, 2f, position, out hit, 25f))
            {
                if (hit.collider.gameObject.CompareTag("agent"))
                {
                    hit.collider.gameObject.GetComponent <BananaAgent>().Freeze();
                }
            }
        }
        else
        {
            myLaser.transform.localScale = new Vector3(0f, 0f, 0f);
        }
    }
    public void MoveAgent(float[] act)
    {
        m_Shoot = false;

        if (Time.time > m_FrozenTime + 4f && m_Frozen)
        {
            Unfreeze();
        }
        if (Time.time > m_EffectTime + 0.5f)
        {
            if (m_Poisoned)
            {
                Unpoison();
            }
            if (m_Satiated)
            {
                Unsatiate();
            }
        }

        var dirToGo   = Vector3.zero;
        var rotateDir = Vector3.zero;

        if (!m_Frozen)
        {
            var shootCommand = false;
            var forwardAxis  = (int)act[0];
            var rightAxis    = (int)act[1];
            var rotateAxis   = (int)act[2];
            var shootAxis    = (int)act[3];

            switch (forwardAxis)
            {
            case 1:
                dirToGo = transform.forward;
                break;

            case 2:
                dirToGo = -transform.forward;
                break;
            }

            switch (rightAxis)
            {
            case 1:
                dirToGo = transform.right;
                break;

            case 2:
                dirToGo = -transform.right;
                break;
            }

            switch (rotateAxis)
            {
            case 1:
                rotateDir = -transform.up;
                break;

            case 2:
                rotateDir = transform.up;
                break;
            }
            switch (shootAxis)
            {
            case 1:
                shootCommand = true;
                break;
            }
            if (shootCommand)
            {
                m_Shoot             = true;
                dirToGo            *= 0.5f;
                m_AgentRb.velocity *= 0.75f;
            }
            m_AgentRb.AddForce(dirToGo * moveSpeed, ForceMode.VelocityChange);
            transform.Rotate(rotateDir, Time.fixedDeltaTime * turnSpeed);
        }

        if (m_AgentRb.velocity.sqrMagnitude > 25f) // slow it down
        {
            m_AgentRb.velocity *= 0.95f;
        }

        if (m_Shoot)
        {
            var myTransform = transform;
            myLaser.transform.localScale = new Vector3(1f, 1f, m_LaserLength);
            var position = myTransform.TransformDirection(RayPerception3D.PolarToCartesian(25f, 90f));
            Debug.DrawRay(myTransform.position, position, Color.red, 0f, true);
            RaycastHit hit;
            if (Physics.SphereCast(transform.position, 2f, position, out hit, 25f))
            {
                if (hit.collider.gameObject.CompareTag("agent"))
                {
                    hit.collider.gameObject.GetComponent <FoodCollectorAgent>().Freeze();
                }
            }
        }
        else
        {
            myLaser.transform.localScale = new Vector3(0f, 0f, 0f);
        }
    }