EDIT:Solved! Thank you so much for your help!
I have a side scrolling game set up with the 2d gameplay tutorial on the unity website.
In my game, I want the player to be able to shoot along the X and Y axis in the direction of the cursor.
The camera's distance isnt a fixed distance from the character, and the character isnt always in the center of the screen.
I want to use raycasting with a ray from the camera to the 2d plane and then a ray from the characters gun to the end of the other ray. Then, if there is a collision, apply the damage and particles etc. to make it look like the weapon was fired.
This is all very confusing to me. I cant figure out how to read the coordinates of the ray shot from the camera to the 2d plane.
static var LOOKING_AT;
function Update ()
{
var mousex = Input.mousePosition.x;
var mousey = Input.mousePosition.y;
//ray from camera to world point at cursor
var ray = camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));
Debug.DrawRay (ray.origin, ray.direction * 15, Color.yellow);
//What is the loc at the end of the ray?
LOOKING_AT = ray.direction * 15;
//15 is the current distance from the camera. I need this to be dynamic.
LOOKING_AT.z=0;
//set to 0 because its on a 2d plane
print (LOOKING_AT);
}
to make it simple, could I just have an invisible object that always follows the cursor, and then shoot a ray from the character to that object? Once I figure this out, I think I can use the tutorials Ive scavenged from all over the web to make the rest of this process easier.
Thanks for helping.