void Start()
    {
        lanes = new List <Transform>();
        GameObject legalLanes = GameObject.FindGameObjectWithTag("LaneTag");

        for (int i = 0; i < legalLanes.transform.childCount; i++)
        {
            lanes.Add(legalLanes.transform.GetChild(i));
        }
        laneDistance         = Vector3.Distance(lanes[0].transform.position, lanes[1].transform.position);
        averageMovementSpeed = laneDistance / shiftTime;
        brakes = GetComponent <PlayerBreaksControl>();

        Messenger.AddListener("GameOver", StopMovement);
        ResetPlayer();
    }
예제 #2
0
    void SetUpStart()
    {
        obstacles = new List <GameObject>();
        float travelBetweenSpawn = blockSpeed * spawnTime / Vector3.Distance(startPoint, endPoint);
        float offset;

        if (obstructPlayer)
        {
            GameObject          player   = GameObject.FindGameObjectWithTag("Player");
            PlayerBreaksControl pbScript = player.GetComponent <PlayerBreaksControl>();
            //How long does it take to travel from the player's current position to the obstacle's current position?
            float playerTravelTime = Mathf.Abs(transform.position.y - player.transform.position.y) / pbScript.defaultMoveSpeed;
            float finalXPos        = player.transform.position.x - playerTravelTime * blockSpeed * Mathf.Sign(-startPoint.x);
            while (Mathf.Abs(finalXPos) > Mathf.Abs(startPoint.x))
            {
                finalXPos += travelBetweenSpawn * Vector3.Distance(startPoint, endPoint) * Mathf.Sign(-startPoint.x);
            }
            offset = Mathf.InverseLerp(startPoint.x, endPoint.x, finalXPos) % travelBetweenSpawn;
        }
        else
        {
            offset = Random.Range(0f, travelBetweenSpawn);
        }
        float startDelay = Mathf.Lerp(spawnTime, 0f, offset / travelBetweenSpawn);

        while (offset < 1.0f)
        {
            GameObject newObstacle = Instantiate(obstaclePrefab, transform.position, Quaternion.identity) as GameObject;
            newObstacle.transform.SetParent(transform);
            newObstacle.transform.localPosition = Vector3.Lerp(startPoint, endPoint, offset);
            newObstacle.transform.Rotate(Vector3.back, obstacleRoatation);
            obstacles.Add(newObstacle);
            offset += travelBetweenSpawn;
        }
        StartCoroutine(SpawnCoroutine(startDelay));
    }
    void StartSetUp()
    {
        //Block the player
        if (obstructPlayer)
        {
            currentTarget = 1;

            GameObject          player   = GameObject.FindGameObjectWithTag("Player");
            PlayerBreaksControl pbScript = player.GetComponent <PlayerBreaksControl>();
            //How long does it take to travel from the player's current position to the obstacle's current position?
            float playerTravelTime = Mathf.Abs(transform.position.y - player.transform.position.y) / pbScript.defaultMoveSpeed;
            //How much time passes between when the blocks starts to leave one waypoint and when it leaves the next waypoint
            float legTime = Vector3.Distance(waypoints[0], waypoints[1]) / speed + waitTime;
            //Eliminate loop
            float offsetTime = playerTravelTime % legTime;

            //Find wether or not player should be waiting
            float timeLeft   = offsetTime;
            float travelTime = Vector3.Distance(waypoints[currentTarget], new Vector3(player.transform.position.x, 0f)) / speed;
            bool  isWaiting  = true;
            bool  flipPos    = false;
            do
            {
                isWaiting = !isWaiting;
                if (isWaiting)
                {
                    timeLeft  -= travelTime;
                    travelTime = legTime - waitTime;
                    flipPos    = !flipPos;
                }
                else
                {
                    timeLeft -= baseWaitTime;
                }
            } while (timeLeft >= 0);

            //In the middle of waiting
            if (isWaiting)
            {
                startedWaitTime = Time.time + timeLeft;
            }
            else
            {
                if (flipPos)
                {
                    obstacle.localPosition = Vector3.Lerp(waypoints[currentTarget], waypoints[1 - currentTarget], Mathf.Abs(timeLeft) / travelTime);
                    currentTarget          = 0;
                }
                else
                {
                    obstacle.localPosition = Vector3.Lerp(waypoints[1 - currentTarget], waypoints[currentTarget], Mathf.Abs(timeLeft) / travelTime);
                }
            }
        }
        else
        {
            float randomPos  = Random.Range(0f, 1f);
            int   prevTarget = Mathf.FloorToInt(randomPos * waypoints.Count);
            currentTarget = (prevTarget + 1) % waypoints.Count;

            float legTime         = (Vector3.Distance(waypoints[0], waypoints[1]) / speed + waitTime);
            float totalTravelTime = legTime * waypoints.Count;
            float finalPos        = randomPos * totalTravelTime % legTime;
            if (finalPos < legTime - waitTime)
            {
                obstacle.localPosition = Vector3.Lerp(waypoints[prevTarget], waypoints[currentTarget], finalPos / (legTime - waitTime));
            }
            else
            {
                obstacle.localPosition = waypoints[currentTarget];
                startedWaitTime        = Time.time - legTime + finalPos;
            }
        }

        startingPosition = waypoints[0].x - blockMovementSpeed * (waitTime * 2f + Vector3.Distance(waypoints[0], waypoints[1]) / speed - transitionWaitTime);
        exitingBlock     = Instantiate(blockPrefab) as GameObject;
        exitingBlock.transform.SetParent(transform.parent);
        exitingBlock.transform.localPosition = Vector3.right * 10f;
        enteringBlock = Instantiate(blockPrefab) as GameObject;
        enteringBlock.transform.SetParent(transform.parent);
        float timeToCheckpoint = 0f;

        if (currentTarget == 1)
        {
            //Waiting at Left
            if (obstacle.transform.localPosition == waypoints[1])
            {
                timeToCheckpoint = 2f * waitTime - Time.time + startedWaitTime + Vector3.Distance(waypoints[0], waypoints[1]) / speed;
                //Moving to Right
            }
            else
            {
                enteringBlock.transform.localPosition = obstacle.transform.localPosition;
                return;
            }
        }
        else if (currentTarget == 0)
        {
            //Waiting at Right
            if (obstacle.transform.localPosition == waypoints[0])
            {
                timeToCheckpoint = waitTime - Time.time + startedWaitTime;
            }
            //Moving to Left
            else
            {
                timeToCheckpoint = waitTime + Vector3.Distance(waypoints[0], obstacle.transform.localPosition) / speed;
            }
        }

        enteringBlock.transform.localPosition = Vector3.left * (Mathf.Abs(waypoints[0].x) + blockMovementSpeed * timeToCheckpoint);

        //pointer.eulerAngles = new Vector3(0f, 0f, Vector3.Angle(Vector3.right, obstacle.transform.localPosition - waypoints[currentTarget]));
    }