how to let my charactere respawn
I started programming my very own Platformer, and now came to a point where i wanted to add a respawn area into my game, if you fall down. so I watched a video on YouTube and recreated it, but its not working in my case. If someone would help me I would appreaciate it a ton : )
So, here is my code, thanks for helping:
First the Player code with the respawn trigger stuff at the bottom: using System.Diagnostics; using UnityEngine; public class Player : MonoBehaviour {
[SerializeField] private LayerMask platformsLayerMask;
private Rigidbody2D rigidbody2d; private BoxCollider2D boxCollider2d; private Animator animator;
public GameManager theGameManager;
public Joystick joystick;
float horizontalMove = 0f;
private void Awake() { rigidbody2d = transform.GetComponent(); boxCollider2d = transform.GetComponent(); }
private void Update() {
if (IsGrounded() && Input.GetKeyDown(KeyCode.Space)) {
float jumpVelocity = 20f;
rigidbody2d.velocity = Vector2.up * jumpVelocity;
}
HandleMovement();
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.CompareTag("Coins"))
{
Destroy(other.gameObject);
}
}
private bool IsGrounded() { RaycastHit2D raycastHit2d = Physics2D.BoxCast(boxCollider2d.bounds.center, boxCollider2d.bounds.size, 0f, Vector2.down, .1f, platformsLayerMask);
return raycastHit2d.collider != null;
}
private void HandleMovement() { float moveSpeed = 12f;
rigidbody2d.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, rigidbody2d.velocity.y);
}
public void ActionJump() {
rigidbody2d.AddForce(transform.up * 400f);
animator.SetTrigger("jump");
}
void onCollisionEnter2D (Collision2D other)
{
if(other.gameObject.tag == "killbox")
{
theGameManager.RestartGame();
}
}
}
And also here my respawn Game Manager code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameManager : MonoBehaviour {
public Transform LevelGenerator;
private Vector3 platformStartPoint;
public Player Player;
private Vector3 playerStartPoint;
// Start is called before the first frame update
void Start()
{
platformStartPoint = LevelGenerator.position;
platformStartPoint = Player.transform.position;
}
// Update is called once per frame
void Update()
{
}
public void RestartGame()
{
StartCoroutine ("RestartGameCo");
}
public IEnumerator RestartGameCo()
{
yield return new WaitForSeconds(2f);
Player.gameObject.SetActive(false);
Player.transform.position = playerStartPoint;
LevelGenerator.position = platformStartPoint;
Player.gameObject.SetActive(true);
}
}
So thank you for helping its really really kind : )
Also I just want to say: yes the Respawn Trigger got the killbox tag, also everything is filled in and yeah I really did everything but it is still not working
Thanks for the help : )
Answer by canslp · Jul 27, 2020 at 12:32 PM
wait so which part isn't working. can you detect collision with the respawn area?
if my charactere falls on the rspawn trigger/ respawn area the box collider works, its visibel but nothing happens