- Home /
OnCollisionEnter2D dont work,OnCollisionEnter2D dont work
hi, bird try to escape from tree, when she collision on tree i want to detect this. i past i do is well in 3d game, but now this my first 2d game. code and some pictures.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BirdMove : MonoBehaviour
{
public float speed = 10f;
public int Life = 3;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position -= transform.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position += transform.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position -= transform.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += transform.right * speed * Time.deltaTime;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log(gameObject.name + "has colleted with " + collision.gameObject.name);
if (collision.gameObject.tag.Equals("target"))
{
Life--;
print("Life you remaining now is: " + Life); ;
}
}
}
One of the GameObject must have an RigidBody2d to identify the collision.. Add RigidBody2d Component To Your Bird
Answer by logicandchaos · Jan 18, 2020 at 02:34 AM
what happens when you play? is your BirdMove script on your bird?
Answer by mscardinal · Jan 18, 2020 at 06:12 PM
I think you should use OnTriggerEnter2D and set the collider of the tree to trigger.
Your answer
Follow this Question
Related Questions
LocalScale doesn't scale sprite to the distance specified 0 Answers
How do I change Raycast's direction based on movement input? 1 Answer
Box and circle Collider doesn't work 1 Answer
How to get GameObject with its tag when ignoring Collision? 2 Answers
2d game end level with trigger and colission with trigger who to make a if condition 1 Answer