Aiming with mouse

Hello everyone :slight_smile:

Still on my little test project and I loose my hairs on this problem, launching missiles and aiming with the mouse cursor.(what a fun program!)

The grey “thing” is the start point of the missiles and the red circle is the sight visor, where the missiles should go.

So the prinpal problem is to convert x/y mouse coordinate to x/y world coordinate I suppose.

I tried a lot of scripts, with a lot of ideas finded on this forum, but nothing work, and I don’t know where to look, just a starting point for testing will be very helpfull.

Thanks in advance :wink:

Hello there.

There’s two api’s that might be of interest to you for your project.

This will return a point in worldspace at the location of the cursor at Z units into the scene.

This will cast a ray from the near plane of the camera that passes through a designated point.

It should be rather easy to use a combination of these to select a target point for your missiles, depending on how you have your red crosshairs implemented.

Hello there,

Thanks for your answer :wink:

I’ve a little problem with the Debug.DrawRay function, when I use it :

Debug.DrawRay (Vector3(transform.position.x,transform.position.y,transform.position.z),Vector3(0,1,0), Color.red);

I did’nt see anything appears, so it’s difficult for me to understand how to use ScreenPointToRay (I’m a practical guy, need to see things working to understand :lol: )

Can you help me on that?

Thanks a lot,

Okay, I had to activate de gizmos in the game view, I will look at it now :smile:

So, I did some tests and can’t come up with something functional :sweat_smile:

I’m a little bit confused about how to use vectors and ScreenToWorldPoint

Here is my code:

if(Input.GetButtonDown("Fire2"))
	{
	
		var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		var hit : RaycastHit;

		if(Physics.Raycast(ray, hit, 10000))
		{
			Debug.Log("hit point:"+hit.point);
			hitPoint=Vector3(hit.point.x,hit.point.y,transform.position.z);
		   
		}
		
		Instantiate(missilePrefab, hitPoint, transform.rotation);

}

so I try to instantiate an instance of my missile prefab in the exact x/y location of the mouse for now.

And I have very strange results with missiles appearing everywhere in function of my main transform position :confused:

Any idea?

So the evolution of my problem:

I feel pathetic. :lol:

I just don’t understand how to position a ray, to start from my player position (in third person view) and to end in the 3d world at the position of the aiming cross.

Is there somewhere a tutorial for using the raycast?

Thanks in advance.

Okay it’s done :mrgreen:
What I didn’t understand was the fact that if the ray didn’t hit something raycast hit return nothing.

So here is my piece of code:

var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		var hit : RaycastHit;
		
		if(Physics.Raycast(ray, hit, 10000))
		{
			Debug.Log("hit point:"+hit.point);
			hitPoint=Vector3(hit.point.x,hit.point.y,0);
			//var cube = GameObject.CreatePrimitive(PrimitiveType.Sphere);
			//cube.transform.position = hit.point;
	   
		}
				missile.LookAt(hit.point);

And the missile has a local force of 200 applied to hit.
Bingo. :lol: