- Home /
Weapon System with collide detection (Helps with script pls)!!!
Im trying to do a collider detection betewn the enemys / environment and the bullet, but i cannot reach it, it just dosent display the Debug.Log, and at consecuence it dosent destroy itself either, pls helps!!, and also but less important, it dosent play the death animation before destroy itself, any idea?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Bullet : MonoBehaviour {
public CaracterController player;
public float velx = 5f;
float velY = 0;
Rigidbody2D rb;
private Animator bullet;
bool stoped = false;
public float lifeTime;
public float distance;
public LayerMask whatIsSolid;
// Use this for initialization
void Start () {
bullet = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
player = FindObjectOfType<CaracterController>();
if (player.transform.localScale.x < 0)
{
velx = -velx;
}
Invoke("AutoDestroy", lifeTime);
}
// Update is called once per frame
private void Update () {
rb.velocity = new Vector2(velx, velY);
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.CompareTag("Enemy"))
{
Debug.Log("LLega el panadero, largo y entero");
AutoDestroy();
}
}
void AutoDestroy()
{
stoped = true;
bullet.SetBool ("Death", stoped);
Destroy(gameObject);
}
}
Hi, be sure that your enemy has the tag Enemy and has a Collider. Your bullet should have a Collider with the box "IsTrigger" set to true in your Collider Component. At least one of your objects (bullet or enemy) must have a rigidbody.
Your answer
Follow this Question
Related Questions
Collision detection not working properly with 2D sprites 1 Answer
Unity2D side collision detection 1 Answer
Collision on specific Frames 1 Answer
2d Raycast problem from enemy to player C# 1 Answer
[c#]collision script not working 1 Answer