Ejemplo n.º 1
0
    void OnTriggerEnter(Collider other)
    {
        Checkpoint otherCheckpoint = other.GetComponent <Checkpoint>();

        if (otherCheckpoint != null)
        {
            if (otherCheckpoint == nextCheckpoint && !finished)
            {
                checkpointTimes[currentCheckpointID] = lapTime;

                //DO CHECKPOINT STUFF
                bool isLastCheckpoint = otherCheckpoint == raceController.checkpoints[raceController.checkpoints.Length - 1];
                bool isFinishLine     = otherCheckpoint == raceController.checkpoints[0];

                currentCheckpoint = otherCheckpoint;

                if (isFinishLine)
                {
                    currentCheckpointID = 0;
                }
                else
                {
                    currentCheckpointID++;
                }
                if (isLastCheckpoint)
                {
                    nextCheckpoint = raceController.checkpoints[0];
                }
                else
                {
                    nextCheckpoint = raceController.checkpoints[currentCheckpointID + 1];
                }

                if (checkpointSound != null && isPlayer)
                {
                    AudioSource.PlayClipAtPoint(checkpointSound, transform.position, 0.5f);
                }
                if (GetComponent <BallControlAI>() != null)
                {
                    PathFollower target = GetComponent <BallControlAI>().GetTarget().GetComponent <PathFollower>();
                    lastCheckpointPosition = target.GetPositionPercentile();
                }

                if (GetComponent <BallControlAI>() != null)               //If ball is AI
                {
                    aiRespawnTime = aiMaxRespawnTime;                     //Auto respawn after 20 secs of not passing a checkpoint.
                }
                //Check if finish line
                if (!isFinishLine)                  //IF NOT FINISH LINE
                {
                    if (isPlayer)
                    {
                        ShowCheckpointTime(false);
                    }
                }
                else                    //IF FINISH LINE
                {
                    if (isPlayer)
                    {
                        ShowCheckpointTime(true);
                    }
                    checkpointTimes = new float[raceController.checkpoints.Length];
                    if (lap < totalLaps)                       //IF ANY LAPS ARE LEFT
                    {
                        lap++;
                        lapTime = 0;
                    }
                    else                         // IF RACE IS FINISHED
                    {
                        finished = true;
                        timerOn  = false;
                        GameObject.Find("RaceController").GetComponent <RaceController>().FinishRacer(this);
                        if (GetComponent <BallControl>() != null)
                        {
                            if (GetComponent <BallControlAI>() != null)
                            {
                                GetComponent <BallControl>().canControl = false;                               //Stop AI balls
                            }
                            //Disable collision with other balls
                            FindObjectOfType <NetworkView>().RPC("SetGhost", RPCMode.All, Network.player);
                        }
                    }
                }

                if (isPlayer)
                {
                    currentCheckpoint.Hide();
                    if (!finished)
                    {
                        nextCheckpoint.Show();
                    }
                }
            }
        }
        if (other.GetComponent <TriggerRespawn>() != null)
        {
            Respawn();
        }
    }