Skip to main content
Code formatting, tagging
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

I have grid movement set up for the player within a set space. The player moves one unit at a time and can move several units when holding down a direction key. I would like the player to respawn back to a respawn point marked by an empty object when the player collides with a hazard's collider.

using UnityEngine;

public class Respawn : MonoBehaviour { [SerializeField] private Transform player; [SerializeField] private Transform respawnPoint;

private void OnTriggerEnter(Collider other) {
    Debug.Log("We've collided!");
    Player p = other.GetComponent<Player>();
    if (p != null)
        player.transform.position = respawnPoint.transform.position;
}

}

using UnityEngine;

public class Respawn : MonoBehaviour {
    [SerializeField] private Transform player;
    [SerializeField] private Transform respawnPoint;

    private void OnTriggerEnter(Collider other) {
        Debug.Log("We've collided!");
        Player p = other.GetComponent<Player>();
        if (p != null)
            player.transform.position = respawnPoint.transform.position;
    }
}

I have grid movement set up for the player within a set space. The player moves one unit at a time and can move several units when holding down a direction key. I would like the player to respawn back to a respawn point empty object when the player collides with a hazard's collider.

using UnityEngine;

public class Respawn : MonoBehaviour { [SerializeField] private Transform player; [SerializeField] private Transform respawnPoint;

private void OnTriggerEnter(Collider other) {
    Debug.Log("We've collided!");
    Player p = other.GetComponent<Player>();
    if (p != null)
        player.transform.position = respawnPoint.transform.position;
}

}

I have grid movement set up for the player within a set space. The player moves one unit at a time and can move several units when holding down a direction key. I would like the player to respawn back to a respawn point marked by an empty object when the player collides with a hazard's collider.

using UnityEngine;

public class Respawn : MonoBehaviour {
    [SerializeField] private Transform player;
    [SerializeField] private Transform respawnPoint;

    private void OnTriggerEnter(Collider other) {
        Debug.Log("We've collided!");
        Player p = other.GetComponent<Player>();
        if (p != null)
            player.transform.position = respawnPoint.transform.position;
    }
}
Source Link

Respawn Player to Respawn point when colliding with a Hazard

I have grid movement set up for the player within a set space. The player moves one unit at a time and can move several units when holding down a direction key. I would like the player to respawn back to a respawn point empty object when the player collides with a hazard's collider.

I have the GridMovement script on the player:

using System.Collections;
using UnityEngine;

public class GridMovement : MonoBehaviour {
    [SerializeField] private float forwardBoundary = 13f;
    [SerializeField] private float leftBoundary = -13f;
    [SerializeField] private float backBoundary = -13f;
    [SerializeField] private float rightBoundary = 13f;

    //[SerializeField] private Transform respawnPoint;

    private bool isMoving;

    //time it takes for player to move from originalPosition to targetPosition. In seconds (so 1/5th of a second)
    private float timeToMove = 0.2f;


    void Update() {

        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) StartMove(Vector3.forward);
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) StartMove(Vector3.left);
        if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) StartMove(Vector3.back);
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) StartMove(Vector3.right);
    }

    private bool StartMove(Vector3 direction) {
        if (isMoving) return false;
        StartCoroutine(MovePlayer(direction));
        return true;
    }

    private IEnumerator MovePlayer(Vector3 direction) {
        isMoving = true;

        float elapsedTime = 0;

        Vector3 originalPosition = transform.position;
        Vector3 targetPosition = originalPosition + direction;

        if (ValidateGridPosition(targetPosition) == targetPosition) {
            while (elapsedTime < timeToMove) {
                transform.position = Vector3.Lerp(originalPosition, targetPosition, (elapsedTime / timeToMove));
                elapsedTime += Time.deltaTime;
                yield return null;
            }
        }
        transform.position = ValidateGridPosition(targetPosition);

        isMoving = false;
    }

    private Vector3 ValidateGridPosition(Vector3 position) {
        if (position.x > rightBoundary) {
            position.x = rightBoundary;
        } else if (position.x < leftBoundary) {
            position.x = leftBoundary;
        } else if (position.z > forwardBoundary) {
            position.z = forwardBoundary;
        } else if (position.z < backBoundary) {
            position.z = backBoundary;
        }
        return position;
    }
}

The player has an empty script on it called Player because it is more scalable than using Tags in Unity. This is the Respawn script on the hazard:

using UnityEngine;

public class Respawn : MonoBehaviour { [SerializeField] private Transform player; [SerializeField] private Transform respawnPoint;

private void OnTriggerEnter(Collider other) {
    Debug.Log("We've collided!");
    Player p = other.GetComponent<Player>();
    if (p != null)
        player.transform.position = respawnPoint.transform.position;
}

}

"We've collided!" displays in the console so I'm guessing the player isn't being teleported back to the respawn point because of Lerp in the GridMovement script? How do I interrupt that?