Example #1
0
    private void CheckIfPlayerInSight(Vector3 playerPos)
    {
        float distToPlayer = Vector3.Distance(transform.position, playerPos);

        // Only check if player is in sight if distance to player is less than threshold
        if (distToPlayer <= CHECK_DIST_TO_PLAYER)
        {
            // Bit shift the index of the layer (8) to get a bit mask
            int layerMask = 1 << 8;

            // This would cast rays only against colliders in layer 8.
            // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
            layerMask = ~layerMask;

            RaycastHit hit;

            if (Physics.Raycast(transform.localPosition, playerPos - transform.localPosition, out hit, CHECK_DIST_TO_PLAYER, layerMask, QueryTriggerInteraction.Ignore) &&
                hit.transform.name.Equals(Player.PLAYER_OBJ_NAME))
            {
                Debug.DrawRay(transform.localPosition, playerPos - transform.localPosition, Color.green);
                pursuePlayerDirectly = true;
                currentWaypoint      = null;
                currentWaypointIndex = 0;
            }
            else
            {
                Debug.DrawRay(transform.localPosition, playerPos - transform.localPosition, Color.red);
                pursuePlayerDirectly = false;
                FindClosestWaypoint();
            }
        }
    }
Example #2
0
    private void MoveToDestination()
    {
        if (destination == null && Path.Waypoints != null)
        {
            destination = FindClosestWayPoint();
        }

        if (destination != null && !IsDisabled)
        {
            if (Vector3.Distance(transform.position, destination.Position) < DIST_TO_DESTINATION)
            {
                destination = FindNextDestination();
            }

            Vector3 target      = destination.Position;
            Vector3 localTarget = transform.InverseTransformPoint(target);

            float angle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
            // Slow down for turns
            speed = Mathf.Abs(angle) > TURN_SLOWDOWN_THRESHOLD ? TURN_SPEED : STRAIGHT_SPEED;

            Vector3    eulerAngleVelocity = new Vector3(0, angle, 0);
            Quaternion deltaRotation      = Quaternion.Euler(eulerAngleVelocity * Time.deltaTime * ROT_TORQUE_MOD);
            Rigidbody.MoveRotation(Rigidbody.rotation * deltaRotation);
        }
        else
        {
            speed = 0;
            Brake(true);
        }
        Move(speed, 0);
    }
Example #3
0
    private PathWayPoint FindNextDestination()
    {
        PathWayPoint        dest = destination;
        List <PathWayPoint> potentialNewDestinations = new List <PathWayPoint>();

        foreach (PathWayPoint wp in destination.ConnectedWayPoints)
        {
            // Check to see if vehicle is facing connected waypoint
            float dot = Vector3.Dot(transform.forward, (wp.Position - transform.localPosition).normalized);
            if (dot >= nextPotentialWaypointDot)
            {
                // Set this back to default, for next waypoint decision
                nextPotentialWaypointDot = DEF_POTENTIAL_DEST_DOT;
                potentialNewDestinations.Add(wp);
            }
        }
        // Determine next destination from given potential ones
        if (potentialNewDestinations.Count > 1)
        {
            int index = Random.Range(0, potentialNewDestinations.Count);
            dest = potentialNewDestinations[index];
        }
        else if (potentialNewDestinations.Count == 1)
        {
            // If only one in potential just grab it
            dest = potentialNewDestinations[0];
        }
        else
        {
            // Try to find a suitable next waypoint, but with a wide DOT threshold this time
            nextPotentialWaypointDot += POTENTIAL_DEST_INCREMENT;
            FindClosestWayPoint();
        }
        return(dest);
    }
Example #4
0
    // Goal is to find the closest waypoint also in our facing direction
    private PathWayPoint FindClosestWayPoint()
    {
        PathWayPoint dest = null;

        foreach (PathWayPoint wp in Path.Waypoints)
        {
            if (dest == null)
            {
                dest = wp;
            }
            if (DEST_FIND_DOT_THRESHOLD > Vector3.Dot(transform.forward, (dest.Position - transform.localPosition).normalized))
            {
                // If current destination is behind truck select this one automatically to avoid back tracking
                dest = wp;
            }
            // Check to see if vehicle is facing waypoint
            float dot = Vector3.Dot(transform.forward, (wp.Position - transform.localPosition).normalized);
            if (dot > DEST_FIND_DOT_THRESHOLD)
            {
                // Check to see waypoint is closer than currently selected destination
                float curDist = Vector3.Distance(transform.position, dest.Position);

                if (curDist > Vector3.Distance(transform.position, wp.Position))
                {
                    dest = wp;
                }
            }
        }
        return(dest);
    }
 private void DecrementDestination()
 {
     if (CurrentDestinationIndex == 0)
     {
         CurrentDestinationIndex = Path.Waypoints.Length - 1;
     }
     else
     {
         CurrentDestinationIndex--;
     }
     Destination = Path.Waypoints[CurrentDestinationIndex];
 }
 private void IncrementDestination()
 {
     if (Destination.lastWayPoint)
     {
         CurrentDestinationIndex = 0;
     }
     else
     {
         CurrentDestinationIndex++;
     }
     Destination = Path.Waypoints[CurrentDestinationIndex];
 }
Example #7
0
    private void MoveToDestination()
    {
        // Check to see if player is closest destination
        Vector3 playerPos = player.transform.position;

        CheckIfPlayerInSight(playerPos);

        if (!pursuePlayerDirectly &&
            Vector3.Distance(transform.position, currentWaypoint.Position) < DIST_TO_DESTINATION)
        {
            // Increment player path if available
            if (currentWaypointIndex - 1 >= 0)
            {
                currentWaypointIndex--;
                this.currentWaypoint = player.PlayerTrailPath.Waypoints[currentWaypointIndex];
            }
            else
            {
                pursuePlayerDirectly = true;
                currentWaypoint      = null;
                currentWaypointIndex = 0;
            }
        }

        Vector3 target      = pursuePlayerDirectly ? playerPos : currentWaypoint.Position;
        Vector3 localTarget = transform.InverseTransformPoint(target);

        float angle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;

        // Slow down for turns
        speed = Mathf.Abs(angle) > TURN_SLOWDOWN_THRESHOLD ? TURN_SPEED : STRAIGHT_SPEED;

        if (sensor.Contact != null && !sensor.Contact.tag.Equals(Player.PLAYER_OBJ_NAME))
        {
            float xPosOfContact = sensor.Contact.transform.position.x;
            angle += xPosOfContact > transform.localPosition.x ? -CONTACT_TURN_MOD : CONTACT_TURN_MOD;
        }

        Vector3    eulerAngleVelocity = new Vector3(0, angle, 0);
        Quaternion deltaRotation      = Quaternion.Euler(eulerAngleVelocity * Time.deltaTime);

        Rigidbody.MoveRotation(Rigidbody.rotation * deltaRotation);
    }
Example #8
0
    private void FindClosestWaypoint()
    {
        if (player.PlayerTrailPath != null &&
            player.PlayerTrailPath.Waypoints != null)
        {
            // Determine Closest Destination
            for (int i = 0; i < player.PlayerTrailPath.Waypoints.Length; i++)
            {
                if (this.currentWaypoint == null)
                {
                    currentWaypointIndex = i;
                    this.currentWaypoint = player.PlayerTrailPath.Waypoints[currentWaypointIndex];
                }

                float curDist = Vector3.Distance(transform.position, this.currentWaypoint.Position);

                if (curDist > Vector3.Distance(transform.position, player.PlayerTrailPath.Waypoints[i].Position))
                {
                    currentWaypointIndex = i;
                    this.currentWaypoint = player.PlayerTrailPath.Waypoints[currentWaypointIndex];
                }
            }
        }
    }
 void Start()
 {
     // Rotate towards destination
     Destination = Path.Waypoints[CurrentDestinationIndex];
     transform.LookAt(Destination.transform);
 }