Example #1
0
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.layer == 21) // 21 = agentlayer
        {
            var otherAgent = other.GetComponent <SquadUnit>();

            if (otherAgent != null)
            {
                if (otherAgent.squadUnitIndex > squadUnitIndex)
                {
                    Vector3 relativeVector = (transform.position - other.transform.position).normalized;
                    _actions.BreakSpeed(1f);
                    _actions.MoveTowards(transform.position + relativeVector, _complexActions.moveSpeed * 10f);
                }
            }
        }
    }
    /// <summary>
    /// Function for iterating trough path and moving towards corners
    /// </summary>
    /// <returns>true if there are more points still, false otherwise</returns>
    public bool GoTowardsDestination(Vector3 destination, bool strafe = true, bool doObstacleRoutine = true)
    {
        Vector3 destinationInNavmesh = destination;

        destinationInNavmesh.y = 0.1f;

        Vector3 forward = sensor.GetVelocity();

        forward.y = 0;
        forward   = forward.normalized;
        Vector3 right = Quaternion.Euler(0, 90, 0) * forward;

        if (sensor.foot != null && sensor.OriginalHeight > 0)
        {
            actions.StandUp(sensor.foot, sensor.OriginalHeight);
        }

        if (pathCornerList.Count <= 0 || (prevDestination - destinationInNavmesh).sqrMagnitude > 0.1f)
        {
            SetDestination(destinationInNavmesh);

            if (pathCornerList.Count <= 0)
            {
                Vector3 samplePos = SamplePosition(destination, 5.0f); // This one seems to be rather heavy
                if (!samplePos.Equals(destination))
                {
                    SetDestination(samplePos);
                }
                if (pathCornerList.Count <= 0)
                {
                    StuckRoutine(right, forward, 6f);
                    cornerIndex = 0;
                    return(false);
                }
            }

            prevDestination = destinationInNavmesh;
        }

        // TODO: Look into why this gives null reference at certain times
        Vector3 diff = transform.position - relativeHeight - pathCornerList[cornerIndex];

        Vector3 diffxz = diff;

        diffxz.y = 0;

        if (diff.magnitude < cornerArriveThreshold ||
            (diffxz.magnitude < cornerArriveThreshold * 0.66f && diff.y - transform.position.y < cornerArriveThreshold * 2f))
        {
            cornerIndex++;
        }

        if (cornerIndex >= pathCornerList.Count)
        {
            pathCornerList.Clear();
            return(false);
        }


        Debug.DrawLine(transform.position, transform.position + forward, Color.red);
        Debug.DrawLine(transform.position, transform.position + right, Color.red);

        if (doObstacleRoutine)
        {
            ObstacleRoutine(diffxz, destination);
        }


        if (cornerIndex >= pathCornerList.Count) // quick fix: because ObstacleRoutine can change path
        {
            pathCornerList.Clear();
            return(false);
        }

        if (!strafe)
        {
            actions.TurnTowards(pathCornerList[cornerIndex], torque, maxTorque, allowedRotationError);
        }

        if (sensor.IsNotMoving) // not stuck
        {
            StuckRoutine(right, forward);
        }
        else
        {
            stuckIteration = -1;
            if (pathCornerList.Count > 0)
            {
                actions.MoveTowards(pathCornerList[cornerIndex], moveSpeed);
            }
        }

        return(true);
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        // ALL of this is debug
        // TODO: remove
        if (doMoveTowards)
        {
            actionSet.MoveTowards(target.position, moveSpeed);
        }
        if (doJump)
        {
            actionSet.Jump(jumpHeight);
            doJump = false;
        }
        if (doPickupRight)
        {
            Physics.IgnoreCollision(GetComponent <CapsuleCollider>(), rightPickupTarget.GetComponent <Collider>());
            actionSet.PickupObject(rightPickupTarget, rightHand, rightHand.Find("WeaponPos"));
            doPickupRight = false;
        }
        if (doPickupLeft)
        {
            Physics.IgnoreCollision(GetComponent <CapsuleCollider>(), leftPickupTarget.GetComponent <Collider>());
            actionSet.PickupObject(leftPickupTarget, leftHand, leftHand.Find("WeaponPos"));
            doPickupLeft = false;
        }

        /*if (doDrop)
         * {
         *  Vector3 velocity = hand.GetComponent<Rigidbody>().velocity;
         *  actionSet.DropObject(pickupTarget, hand.transform, velocity);
         *  doDrop = false;
         * }*/
        if (doActivateRight)
        {
            actionSet.ActivateObject(rightPickupTarget, rightHand.Find("WeaponPos"));
        }
        if (doActivateLeft)
        {
            actionSet.ActivateObject(leftPickupTarget, leftHand.Find("WeaponPos"));
        }
        if (doAimAtRight)
        {
            rightSlerpPos += 0.2f;
            actionSet.AimAt(rightHand, target.position, rightSlerpPos);
            if (rightSlerpPos > 1)
            {
                rightSlerpPos = 0;
            }
            //doAimAtRight = false;
        }
        if (doAimAtLeft)
        {
            leftSlerpPos += 0.2f;
            actionSet.AimAt(leftHand, target.position, leftSlerpPos);
            if (leftSlerpPos > 1)
            {
                leftSlerpPos = 0;
            }
        }
        if (doCrouch)
        {
            actionSet.Crouch(leg, originalHeight, 0.3f);
            doCrouch = false;
        }
        if (doStandup)
        {
            actionSet.StandUp(leg, originalHeight);
            doStandup = false;
        }
        if (doReload)
        {
            actionSet.ReloadWeapon(rightPickupTarget);
            actionSet.ReloadWeapon(leftPickupTarget);
            doReload = false;
        }
        if (doOrientate)
        {
            actionSet.TurnTowards(target, 1.0f, 2.0f, 7f);
        }
        if (setDestinationWithStrafe)
        {
            setDestinationWithoutStrafe = false;
            setDestinationWithStrafe    = activitySet.GoTowardsDestination(destination.position);
            actionSet.TurnTowards(destination, 1.0f, 2.0f, 7f);
        }
        if (setDestinationWithoutStrafe)
        {
            setDestinationWithStrafe    = false;
            setDestinationWithoutStrafe = activitySet.GoTowardsDestination(destination.position, false);
            //actionSet.TurnTowards(destination, 1.0f, 2.0f, 7f);
        }
        //animator.SetInteger("RoutineStatus", routineStatus);
    }