- Home /
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.
Tip: Use Debug.DrawRay or Debug.Drawline to see your ray. Use the .point variable of your raycasthit to get the world position your raycast hit (if any).
I tried drawing the ray and drawing at a line with the parameters that you suggested and i cant seem to see the lines at all, i made sure it was in the update function but still no line.
Debug draw functions are only visible in the Editor window, so you'll have to have it visible while you test to see it.
Answer by brazmogu · Oct 13, 2015 at 08:30 PM
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, i want to make the laser shoot out of the player from the position it is at to the in the direction that it is facing, i got the graphics working its just the raycasting that isnt working.
If you're rotating the player transform, you can use transform.right, which is the "right" vector from the point of view of the player.
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.right);
Wait, here's another thing I don't get: if you want to make the player shoot in the direction it's facing, then what does the mouse position have to do with that?
Answer by Eno-Khaon · Oct 14, 2015 at 07:34 PM
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);
Your answer
Follow this Question
Related Questions
Unity 2D Top-Down Raycast length/distance and direction problem 0 Answers
2D Topdown movement without sliding 2 Answers
2D Top Down Game, 2D or 3D defaults? 1 Answer
2D Topdown Shooter Enemy Knockback 0 Answers
(2d - top down) Characters drifting upon movement,Characters drifting upon movement (2d) 1 Answer