- Home /
shooting laser towards mouse
I'm trying to make a 2D game with a laser that the player can shoot that reflects off of mirrors, but I'm kinda new to Unity.
It all works, except that I can't get the player to fire the laser at the mouse position. I have no trouble getting the mouse position using:
Vector3 screenPos = Input.mousePosition;
screenPos.z = 0;
Vector3 worldPos = Camera.mainCamera.ScreenToWorldPoint(screenPos);
But simply using the LookAt() function like all the tutorials show will not work for me. Because of the equation I'm using to calculate reflection, I need a single value out of 360 to define the direction the laser is moving in. I'm going with the EulerAngle.z value. So basically, what I need is to get the direction from the player's x and y position to the mouse position in a single value out of 360. Then I can just set the laser's eulerAngles.z rotation to that.
Any help of any kind would be appreciated, especially in C# as that is the language I'm using.
Answer by MountDoomTeam · Oct 14, 2012 at 06:57 AM
try just getting your program to print the start pt, end pt and the vector line of both of the points.
it's a FPS view? Ok, your explanation was making sense, and then the question became analytically incoherent and confused, like you dont know the 3d x y z stuff.
if you need a single value out of 360, you need an int. if you need a single value out of 3d eulerAngles x.y.z then you need just a value on the z azis, like a point in space with y z = 0.
cos you talk of geometry you have to be clear on points, lines, surfaces, like planes, .
you need the player point, here u are using the camera so FPS. then you need the point you aim at. it is another XYZ point in 3d.
Then you have to get the 2 points and make a line coordinate,
search answers and reference for "make a line from 2 points" it's a kind of vector3 in unity , for lines and points. it's called a vector, and it can can have direction info or no, like going from A to B or B to A. also learn to do this:
to be honest you arent thinking about the program clearly, it's geometry, you need point a and b, their line, the angle of the mirror, the difference between the euler of the mirror and the lazer, and do the maths on that. that's what you are asking us. the points a and b are x y z, the vector is x y z is a vector, and the rotations are also xyz. print all their values and understand the graph data in x y z.