I have a bullet object that I’m trying to get to collide with 2 different sets of objects. I separate the two types of objects using two different tags, one is moveable thus allowing the bullet to apply velocity to the collided object. The other is static like the ground. For some reason collisions with the moveable objects work but not the collisions with the static objects and I don’t know why. I’ve made sure to attach 2d Box Colliders to all the objects. Any help would be greatly appreciated since I am still very new to unity. My bullet script is attached below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour {
public Transform gunTransform;
public float speed = 5f;
public float lifeTime = 5f;
Vector2 moveVector;
public void Start()
{
gunTransform = GameObject.Find("PlayerGun").transform;
moveVector = gunTransform.rotation * Vector2.right;
moveVector /= moveVector.magnitude;
}
void FixedUpdate()
{
transform.Translate(moveVector * speed * Time.deltaTime);
}
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("Colliding");
//Not Working, Static objects don't even registor collisions
if (collision.collider.tag == "StaticObstacle")
{
Debug.Log("Hit Static");
Destroy(gameObject);
}
//Working Script
if (collision.collider.tag == "MoveableObstacle")
{
collision.collider.GetComponent<Rigidbody2D>().velocity = moveVector * speed;
Destroy(gameObject);
}
}
void Update()
{
lifeTime -= Time.deltaTime;
if (lifeTime <= 0)
{
Object.Destroy(gameObject);
}
}
}