So I’m making a 2 player game where you spawn units to battle each other in a tug-of-war type fashion. It’s between fire and ice. Only problem is, I haven’t found a way to make it so that my projectiles collide with enemy spawns (the imps or snowmen) but pass through other projectiles. Because of this, the projectiles push each other around and very rarely actually reach their intended target.
I’m certain this isn’t the best way of doing things, but I don’t know what is. Should I be using OnTrigger somehow instead? I attached my Projectile script and some pictures, let me know if there’s anything else you need.
{
public float moveSpeed = 10f;
public int damageDealt = 20;
Vector2 movement;
private Rigidbody2D rb;
int direction;
string enemy;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
movement = new Vector2(moveSpeed, 0f);
if (tag == "Fire Projectile")
{
direction = -1;
enemy = "Ice";
}
else if (tag == "Ice Projectile")
{
direction = 1;
enemy = "Fire";
}
else
{
Debug.Log("No tag chosen on " + this.gameObject.name);
}
}
// Update is called once per frame
void Update()
{
rb.velocity = (movement * direction);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == enemy)
{
collision.gameObject.GetComponent<EntityBehaviour>().TakeDamage(damageDealt);
}
}
}