Exemple #1
0
    public bool LookTowards()
    {
        // Get rotation vector for left and right direction
        if (!m_isRotationDone)
        {
            m_left           = transform.position + Vector3.Cross(transform.forward, transform.up);
            m_right          = transform.position + Vector3.Cross(transform.up, transform.forward);
            m_forward        = transform.position + transform.forward;
            m_isRotationDone = true;
        }
        // Look left and right
        switch (m_lookRightLeft)
        {
        case 0:     // Look left
            if (m_movement.LookInDirection(m_left))
            {
                m_lookRightLeft++;
            }
            break;

        case 1:     // Look forward
            if (m_movement.LookInDirection(m_forward))
            {
                m_lookRightLeft++;
            }
            break;

        case 2:     // Look right
            if (m_movement.LookInDirection(m_right))
            {
                m_lookRightLeft++;
            }
            break;

        case 3:     // Look forward
            if (m_movement.LookInDirection(m_forward))
            {
                m_lookRightLeft++;
            }
            break;

        default:     // Finished looking around
            m_lookRightLeft  = 0;
            m_isRotationDone = false;
            return(true);
        }
        return(false);
    }
Exemple #2
0
    IEnumerator FollowPath()
    {
        //Debug.Log("PATHUNIT: Inside FollowPath!");
        if (path == null)
        {
            //Debug.Log("PATHUNIT: No path found :( ");
            yield return(null);
        }
        else if (path.Length == 0)
        {
            //Debug.Log("PATHUNIT: Array is empty");
            yield return(null);
        }

        // Check if a path exists and have at least one value
        if (path != null && path.Length > 0)
        {
            // Start at the first point
            Vector3 currentWaypoint = path[0];
            while (true)
            {
                // Check if we are at the current point
                if (transform.position == currentWaypoint)
                {
                    targetIndex++;
                    if (targetIndex >= path.Length)
                    {
                        targetIndex = 0;
                        path        = new Vector3[0];
                        yield break;
                    }
                    // Update waypoint to the next point
                    currentWaypoint = path[targetIndex];
                }
                // Disable smooth rotation
                //movement.isSmoothRotation = true;

                // Look towards current waypoint
                movement.LookInDirection(currentWaypoint);

                // Move towards the current waypoint
                transform.position = Vector3.MoveTowards(transform.position, currentWaypoint, speed * Time.deltaTime);
                yield return(null);
            }
        }
    }