示例#1
0
    IEnumerator ControlFollowers()
    {
        while (true)
        {
            float x = -3;
            float y = 1;
            for (int i = 0; i < followers.Count; i++)
            {
                x += 2;
                FollowerAI fol = followers[i];
                if (fol == null)
                {
                    continue;
                }
                Vector3 targetPos = Vector3.zero;
                targetPos.z += y * followerPosFactor;
                targetPos.x += x * followerPosFactor;
                fol.SetTarget(transform.TransformPoint(-targetPos));



                if (x == y)
                {
                    y++;
                    x = -y - 2;
                }
            }
            yield return(new WaitForSeconds(0.2f));
        }
    }
    void Start()
    {
        if (lockCursor)
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible   = false;
        }

        target = Human;
        other  = Dog;

        offset = new Vector3(0, dstFromTarget, -dstFromTarget);

        targetCM = target.GetComponent <CharacterMovement>();
        targetCC = target.GetComponent <CharacterController>();
        otherFAI = other.GetComponent <FollowerAI>();
        otherNVA = other.GetComponent <NavMeshAgent>();
    }
    void SwapTarget()
    {
        //Om "target" och "other" är inom ett visst avstånd från varandra.
        if (Vector3.Distance(target.transform.position, other.transform.position) < maxSwapDistance)
        {
            targetCM.enabled = false;
            targetCC.enabled = false;
            otherFAI.enabled = false;
            otherNVA.enabled = false;

            float targetFacing = Vector3.Angle((other.transform.position - target.transform.position), target.transform.forward);
            float otherFacing  = Vector3.Angle((target.transform.position - other.transform.position), other.transform.forward);

            //Så länge "target" inte tittar på människan.
            if (targetFacing > minAngleDiffForSwap)
            {
                Vector3 dirFromTargetToOther = other.transform.position - target.transform.position; dirFromTargetToOther.Normalize();
                dirFromTargetToOther.y = 0f;
                Quaternion lookRotationDog = Quaternion.LookRotation(dirFromTargetToOther);
                target.transform.rotation = Quaternion.Lerp(target.transform.rotation, lookRotationDog, Time.deltaTime * 2);
            }

            //Så länge "other" inte tittar på hunden.
            if (otherFacing > minAngleDiffForSwap)
            {
                Vector3 dirFromOtherToTarget = target.transform.position - other.transform.position; dirFromOtherToTarget.Normalize();
                dirFromOtherToTarget.y = 0f;
                Quaternion lookRotationHuman = Quaternion.LookRotation(dirFromOtherToTarget);
                other.transform.rotation = Quaternion.Lerp(other.transform.rotation, lookRotationHuman, Time.deltaTime * 2);
            }

            //Roterar kameran från den ena targeten till den andra.
            if (targetFacing < minAngleDiffForSwap && otherFacing < minAngleDiffForSwap)
            {
                Vector3 newCamTargetPos = other.transform.position - other.transform.forward * dstFromTarget;

                //Kameran kollar om den har bytat position till sitt nya target, och bytar target isåfall.
                if (currentLerpTime >= 1)
                {
                    if (target == Dog)
                    {
                        target = Human;
                        other  = Dog;
                    }
                    else
                    {
                        target = Dog;
                        other  = Human;
                    }

                    targetCM = target.GetComponent <CharacterMovement>();
                    targetCC = target.GetComponent <CharacterController>();
                    otherFAI = other.GetComponent <FollowerAI>();
                    otherNVA = other.GetComponent <NavMeshAgent>();

                    Vector3    relativePos = target.transform.position - transform.position;
                    Quaternion rotation    = Quaternion.LookRotation(relativePos, Vector3.up);

                    pitch = rotation.eulerAngles.x;
                    yaw   = rotation.eulerAngles.y;

                    targetRotation  = rotation.eulerAngles;
                    currentRotation = rotation.eulerAngles;

                    targetCM.enabled = true;
                    targetCC.enabled = true;
                    otherFAI.enabled = true;
                    otherNVA.enabled = true;
                    swappingTarget   = false;
                    justSwitched     = true;
                    currentLerpTime  = 0;
                }
                //If the camera hasn't rotated to the new target.
                else
                {
                    Vector3 tempCamTargetPos = target.transform.position + (target.transform.forward * (Vector3.Distance(target.transform.position, other.transform.position) / 2));
                    currentLerpTime += Time.deltaTime;
                    transform.LookAt(tempCamTargetPos);
                    transform.position = Vector3.Lerp(transform.position, newCamTargetPos, currentLerpTime / lerpTime);
                    targetRotation     = tempCamTargetPos;
                }
            }
        }
        else
        {
            swappingTarget = false;
        }
    }