コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        #region here is something dont need
        //if (isFlying == true)
        //{
        //    Debug.Log("start to fly");
        //    rb.velocity = new Vector3(0, 1f, 0);
        //}


        //if (transform.position.y <= 2)
        //{
        //    isFlying = true;
        //}
        //else
        //{
        //    isFlying = false;
        //}

        #endregion

        #region Code that let turret move up and down

        if (m_isUp == true)
        {
            rb.AddForce(transform.up * 2f);
        }
        else
        {
            rb.AddForce(-transform.up * 2f);
        }

        if (isUp == true)
        {
            Debug.Log("Start flying");
            //rb.velocity = new Vector3(0, 1f, 0);
            rb.AddForce(transform.up * 2f);
        }


        if (isDown == true)
        {
            Debug.Log("Go down");
            //rb.velocity = new Vector3(0, -10f, 0);
            rb.AddForce(-transform.up * 2f);
        }

        if (transform.position.y > 6)
        {
            if (upCoroutine == null)
            {
                upCoroutine = StartCoroutine("BeginUp");
            }
            else
            {
                StopCoroutine(upCoroutine);
                upCoroutine = null;
            }
        }

        if (transform.position.y < 0)
        {
            if (downCoroutine == null)
            {
                downCoroutine = StartCoroutine("BeginDown");
            }
            else
            {
                StopCoroutine(downCoroutine);
                downCoroutine = null;
            }
        }

        if (transform.position.y > 7)
        {
            transform.rotation = Quaternion.Euler(0f, m_rotation.y, 0f);
        }

        //if(transform.position.y < 2 && transform.rotation.x > 3)
        //{
        //    transform.rotation = Quaternion.Euler(0, m_rotation.y, 0f);
        //}


        #endregion

        #region Code that make the AI wander

        if (isWandering == false)
        {
            //StartCoroutine("Wander");
            if (wanderCoroutine == null)
            {
                wanderCoroutine = StartCoroutine("Wander");
            }
            else
            {
                StopCoroutine(wanderCoroutine);
                //Debug.Log(wanderCoroutine);
                wanderCoroutine = null;
            }
        }

        if (isRotatingRight == true)
        {
            rb.angularVelocity = new Vector3(0, rotationSpeed, 0);
        }
        else
        {
            rb.angularVelocity = Vector3.zero;
        }


        if (isRotatingLeft == true)
        {
            rb.angularVelocity = new Vector3(0, -rotationSpeed, 0);
        }
        else
        {
            rb.angularVelocity = Vector3.zero;
        }



        if (isMovingForward == true)
        {
            Debug.Log("walking");
            //rb.AddForce(transform.forward * moveSpeed); //use addforce it wont move up when it rotation is looking up already


            rb.velocity = transform.forward * moveSpeed;
        }
        else
        {
            Debug.Log("Not walking");
            rb.velocity = Vector3.zero;
        }
        #endregion


        //if (m_target != null) // this line of code need to be in a class and when shooting call this code in wanderAiForShooting. 16/8/2020 remember to fix it
        //{
        //    isMovingForward = false;
        //}



        /*
         * if boxcast hit border   (done)
         *      if boxcast hit wall (done)
         *          call avoid obstacle code to rotate and look for nearest place to rotate than set it to rotate(done)
         *          save vector3 end point of boxcast on wall into a temporary data (done)
         *      if AI pass true the wall call true (done)
         *          set the vector3 end point direction for AI to rotate
         * else if boxcast hit wall
         *      call rotate and look for nearest place to rotate
         *
         * if distance is less than vector3 of end point boxcast
         *      set vector3 of end_of_position back to null
         *
         */
        if (Physics.BoxCast(center: transform.position, halfExtents: transform.lossyScale / 2, direction: transform.forward,
                            out secondHit, transform.rotation, raycastMaxDistance, layerMask1))
        {
            //save the secondhit.point into a temporary data here
            if (Physics.BoxCast(center: transform.position, halfExtents: transform.lossyScale / 2, direction: transform.forward,
                                out hit, transform.rotation, maxDistance / 2, layerMask))
            {
                targetTransform = avoidObstacle.DetectTurret(transform.position);
                m_moveTowardTarget.SetTarget(targetTransform);

                Debug.Log(end_of_position);
                end_of_position = secondHit.point;
            }
            if (to_rotate == true)
            {
                rttObject.SetTempDirection(end_of_position);
                to_rotate = false;
            }
            if (transform.position == end_of_position)
            {
                Debug.Log("set vecotr3 end of position to null");
            }
        }

        else if (Physics.BoxCast(center: transform.position, halfExtents: transform.lossyScale / 2, direction: transform.forward,
                                 out hit, transform.rotation, maxDistance / 2, layerMask))
        {
            targetTransform = avoidObstacle.DetectTurret(transform.position);
            m_moveTowardTarget.SetTarget(targetTransform);
        }

        //if (Physics.BoxCast(center: transform.position, halfExtents: transform.lossyScale / 2, direction: transform.forward,
        //    out secondHit, transform.rotation, raycastMaxDistance/4, layerMask1))
        //{
        //    rb.angularVelocity = new Vector3(0, rotationSpeed - 90, 0);
        //}



        #region here is to make the turret turn 90 degre when it detect wall (close already)
        //here is to let the turret avoid wall

        /*
         * if (isRaycasting == true)
         * {
         *  if (Physics.BoxCast(center: transform.position, halfExtents: transform.lossyScale / 2, direction: transform.forward,
         *  out hit, transform.rotation, raycastMaxDistance, layerMask1))
         *  {
         *      Debug.Log("is ON");
         *      end_of_position = hit.point;
         *      Debug.Log(end_of_position);
         *      rttObject.SetTempDirection(end_of_position);
         *      //rb.angularVelocity = new Vector3(0, rotationSpeed - 90, 0);
         *  }
         * }
         *
         */



        #endregion

        #region

        /*
         *
         * //here is use raycast physics to tell the turret turn
         *
         *
         * if (Physics.BoxCast(center: transform.position, halfExtents: transform.lossyScale / 2, direction: transform.forward,
         * out hit, transform.rotation, maxDistance, layerMask))
         * {
         *  targetTransform = avoidObstacle.DetectTurret(transform.position);
         *
         * // Debug.Log(" after call avoid obstacle set to" + targetTransform);
         * //m_rotateObject.SetTarget(targetTransform);//change it, pass the value back to the sensorAI -> m_target there
         *  m_moveTowardTarget.SetTarget(targetTransform);
         * }
         */


        //this line need to change because it prevent the object ot rotate upward if it didnt see enemy 22/8/2020
        ////here is making the turret direction x face 0 after shoot
        //if (transform.rotation.x != 0 && m_target == null)
        //{
        //    transform.rotation = Quaternion.Euler(0f, m_rotation.y, 0f);
        //}

        //float maximumDistance = float.MaxValue;

        //float distance = Vector3.Distance(gameObject.transform.position, border.transform.position);
        //if (distance < maximumDistance)
        //{
        //    moveTargetPosition = border.transform;
        //}
        #endregion


        #region thing dont need
        //if (m_distance < 5 && m_distance > 1)
        //{
        //    rb.velocity = Vector3.zero;
        //    m_target = null;
        //    Debug.Log(m_distance + " the distance suppose to be less than 5 and the turret movement is set to zero");
        //}
        #endregion
    }