Trying to make a topdown 2d laser

if (Input.GetMouseButton (1)) {
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right);

			laser.enabled = true;
			laser.SetVertexCount(2);
			laser.SetPosition(0, new Vector3(0, 0, transform.position.z));
			laser.SetPosition(1, mousePos);

			if(hit.collider != null) {
				eController.eCurHealth -= laserDamage;
			}
		}

This is what i have so far and it points the laser in the correction direction (from the player’s position to the mouse’s position) but the raycasting doesn’t seem to work and it does not collide with the enemy player and destroy them. I am relatively new to unity so I am not sure if I missed anything, any help will be appreciated, thanks.

I’m sincerely not sure what are you doing here. Your raycast is only pointing to the right(0,1), and then you’re acctivatin laser, which I think is an Edge Collider? Then what are you using Raycast for?

My suggestion is that you change the direction in the Raycast call by setting the direction to the direction from the player to the enemy using Vector3.Angle, and if you’re still having problems I suggest checking if the layer of the enemy is set to accept raycasting, and I’d also suggest that you use the LayerMask parameter in your raycast call.

its a 2d scene so and i read somewhere
that vector2.right was forward in the
direction that the sprite is facing

That’s a bit of a misconception, really.

While that can be true if your character is a right-facing sprite and you’re moving to the right, for any other case, it’s not anymore.

That aside, you appear to be using the mouse to create the visual effect of your shot, so why not also use it for your raycast?

If your line renderer (laser) is already displaying correctly when using your “mousePos” variable, you should only need to treat that as a relative direction for your raycast.

// C#

RaycastHit2D  hit = Physics2D.Raycast(transform.position, (mousePos - transform.position).normalized, (mousePos - transform.position).magnitude);