- Home /
Question by
Lava_Key · Apr 05, 2020 at 02:22 PM ·
2d gameontriggerenter
OntriggerEnter isn't working.
I am currently make a 2d game were the player has to dodge randomly spawning obstacles. I'm following Blackthornprod's unity tutorial and I don't know why this code to check if the player is touching the obstacle isn't working. Does anyone know how to fix this?
Heres the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Obstacle : MonoBehaviour
{
public int damage = 1;
public float speed;
// Start is called before the first frame update
private void Update()
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
}
// Update is called once per frame
void OntriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
other.GetComponent<Player>().health -= damage;
Debug.Log(other.GetComponent<Player>().health);
Destroy(gameObject);
}
}
}
Comment
Answer by gaminics_burkan · Apr 05, 2020 at 02:47 PM
Make sure, your gameObject's isTrigger checkbox clicked.
Answer by Bmarlyman21 · Apr 05, 2020 at 02:52 PM
Try adding a rigidbody to one of the objects. It is required for a trigger function to work.
Your answer