- Home /
Having problems with obstacle collision killing the player
I know on the surface this is a ridiculously nooby question which has plenty of tutorials online for it, however I have tried following all of them and I cannot for the life of me get it to work. I'm working on a small endless runner for Android and I have multiple obstacles in the game, all using the tag Obstacle. I have a player with the Player tag also. I want the player to immediately die when coming into contact with an obstacle. Easy right?
However, none of the methods I've tried to implement have been successful. When I tried to implement it with health and damage it never registered it in the debug log, yet it must have identified the tag as it destroyed the game object. I've now followed another tutorial where it links together with bringing up a game over screen and having the option to restart, and in this one it was advised to not destroy the object but to use SetActive(false). Now the player just goes through the obstacles and nothing happens. My obstacle collisions are triggers and their script only contains the functionality for them to move across the screen. Nothing else.
Here is the entirety of my Player script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Player : MonoBehaviour
{
public Animator animator;
public UnityEvent OnLandEvent;
public float Jumpforce;
private float moveinput;
private Rigidbody2D Myrb2d;
private bool isGrounded;
public Transform groundcheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJumps;
public int extraJumpsValue;
public GameObject gameOverText, restartButton;
// Start is called before the first frame update
void Start()
{
gameOverText.SetActive(false);
restartButton.SetActive(false);
extraJumps = extraJumpsValue;
Myrb2d = gameObject.GetComponent<Rigidbody2D>();
animator = gameObject.GetComponent<Animator>();
if (OnLandEvent == null)
OnLandEvent = new UnityEvent();
}
// Update is called once per frame
void FixedUpdate(){
isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, whatIsGround);
}
private void Update(){
if(isGrounded == true){
extraJumps = extraJumpsValue;
OnLandEvent.Invoke();
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && extraJumps > 0) {
Myrb2d.velocity = Vector2.up * Jumpforce;
extraJumps--;
isGrounded = false;
animator.SetBool("IsJumping", true);
} else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && extraJumps == 0 && isGrounded == true) {
Myrb2d.velocity = Vector2.up * Jumpforce;
}
}
void OnCollisionEnter2D (Collision2D other)
{
if (other.gameObject.tag.Equals("Obstacle"))
{
gameOverText.SetActive(true);
restartButton.SetActive(true);
gameObject.SetActive(false);
}
}
public void OnLanding()
{
animator.SetBool("IsJumping", false);
}
}
Answer by BradyIrv · Jun 08, 2019 at 07:18 PM
If your obstacle colliders are set to Triggers, then you have to use this method:
private void OnTriggerEnter2D(Collider2D other) {
// Also instead of tag.Equals(), you can just say:
if (other.transform.tag == "Obstacle") {
// Do something
}
}
// As opposed to using the method OnCollisionEnter2D