// Update is called once per frame
    void Update()
    {
        const float TOLERANCE      = 0.001f;
        Vector2     sourcePosition = body.transform.position;
        Vector2     targetPosition = currentCheckpoint.transform.position;
        Vector2     offset         = targetPosition - sourcePosition;
        float       distance       = offset.magnitude;

        if (TOLERANCE < distance)
        {
            Vector2 direction      = offset / distance;
            Vector2 velocity       = speed * direction.normalized;
            Vector2 nextPosition   = sourcePosition + Time.deltaTime * velocity;
            Vector2 targetToSource = sourcePosition - targetPosition;
            Vector2 targetToNext   = nextPosition - targetPosition;
            if (active)
            {
                //transform.right = direction;
                if (0.0f < Vector3.Dot(targetToSource, targetToNext))
                {
                    body.velocity = velocity;
                }
                else
                {
                    currentCheckpoint = currentCheckpoint.Path.GetNextCheckpoint(currentCheckpoint);
                }
            }
        }
        else
        {
            currentCheckpoint = currentCheckpoint.Path.GetNextCheckpoint(currentCheckpoint);
        }
    }
Beispiel #2
0
    public static CheckpointBehaviour GetClosestCheckpoint(Vector3 position)
    {
        List <CheckpointBehaviour> checkpoints = FindObjectsOfType <CheckpointBehaviour>().ToList();
        float closestDistance = Vector3.Distance(position, checkpoints[0].transform.position);
        CheckpointBehaviour closestCheckpoint = checkpoints[0];

        for (int i = 1; i < checkpoints.Count; i++)
        {
            CheckpointBehaviour potentialCheckpoint = checkpoints[i];
            float potentialDistance = Vector3.Distance(position, potentialCheckpoint.transform.position);
            if (potentialDistance < closestDistance)
            {
                closestCheckpoint = potentialCheckpoint;
                closestDistance   = potentialDistance;
            }
        }
        return(closestCheckpoint);
    }
Beispiel #3
0
    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.tag == "Checkpoint")
        {
            checkpointBehaviour = col.gameObject.GetComponent <CheckpointBehaviour>();

            if (!checkpointBehaviour.isAktivated)
            {
                currentSpawnPoint = col.transform;
                checkpointBehaviour.isAktivated = true;
                checkpointBehaviour.ChangeSprite();

                information.SaveOrbs();

                FindObjectOfType <SoundManager>().Play("CheckpointAktivated");
                print("Setting Spawnpoint to : " + col.name);
            }
        }
    }
Beispiel #4
0
    private void OnDeath()  //Seperating this from OnTriggerEnter so that I can use trigger volumes for other purposes
    {
        CheckpointBehaviour plugin = m_CheckpointManager.GetComponentInChildren <CheckpointBehaviour>();

        int respawnIndex = 0;

        if (plugin.m_CurrentCheckpoint > 0)
        {
            respawnIndex = plugin.m_CurrentCheckpoint - 1;
        }

        //this.transform.position = plugin.m_CheckpointLocations[respawnIndex].transform.position + (Vector3.up * m_SpawnHeight);

        this.transform.SetPositionAndRotation(plugin.m_CheckpointLocations[respawnIndex].transform.position + (Vector3.up * m_SpawnHeight), Quaternion.identity);

        Debug.Log("hah you suck");

        //TODO: add a visual tell instead of just teleporting the player
    }
    public CheckpointBehaviour GetNextCheckpoint(CheckpointBehaviour checkpoint)
    {
        int i = checkpoints.IndexOf(checkpoint);

        return(checkpoints[(i + 1) % checkpoints.Count]);
    }