Shooting on both sides w/ raycast

Hi!
I’m making a simple 2D platformer game. I have a character that moves right and left,jumps,climbs ropes and dashes. How do I make the character shoot enemies on both sides (at the same time,by pressing V), using raycast? I do not need to make him shoot bullet prefabs, just making the raycast hit enemies on both sides is enough. I’ll be adding a shooting animation later.
Thanks!

You raycast in two directions, i dont hink theres an easier mthod

Yeah I figured, but how do you write the code? xD I’m a total newbie,tried googling it before but couldn’t find it

Basically if you have an enemy script attached to the enemy, and it has a public method called registerHit(float damage), then your vode would look like this

Void hitIn2Dir () {
   HitInfo hit;
   If (Physics. Raycast(transform.position,transform.forward, out hit))  {
      Enemy enemy = hit.gameObject.GetComponent<Enemy>();
      If (enemy != null)
         enemy.registerHit(damage);
   }
}

repeat it with transform.back,and youre done

Okay thanks a ton,I’ll try that out :slight_smile:

If you don’t know how to write the code, look at other raycast examples and look at the documentation. Knowing how to do it will make your life far easier when writing more complicated code later.

2 Likes