Converting mouse position from pixels to position ? RTS bug

I posted the basic code below. 90% of the time it works fine and destination is changed 10% of the time it does not change the destination. This seems to happen when the transform is at right angles to the new destination. This is basically the same code as the community RTS project which also has this problem. http://forum.unity3d.com/viewtopic.php?t=18802&start=0

I think it is from converting mouse position, which is in pixels to a x, y position. Anyone have any suggestions?

I could use RaycastAll and see if terrain is hit anywhere along the ray but I do not think that is the problem.

private Vector3 destination;
private bool moving = false;
private int terrainLayerMask = 1 << 8; //only vs layer 8

 void FixedUpdate()
    {
		
		   // Left mouse button
        if (Input.GetButtonUp("Fire1"))
        {
            Mouse1Down(Input.mousePosition);
        }
}
 
        private void Mouse1Down(Vector2 screenPosition) 
    {
		
	

		Vector3 where = new Vector3(screenPosition.x, screenPosition.y, 0);

		
		RaycastHit hit;
		
        Ray ray = Camera.main.ScreenPointToRay(where);
       
     
        if(Physics.Raycast(ray, out hit, 1000, terrainLayerMask))
        {

            destination = hit.point;
			destination.x = (float)Math.Round(destination.x);
			destination.y = (float)Math.Round(destination.y);
			Debug.Log (destination);
			
					
moving = true;

          
           
        }
       
    }

Have you tried the latest 1.1 version? This fixes the 10% not precisely working issue. The issue was that in those cases the mouse down and up point were not exactly the same, so it drew a mini non-visible 1-2px rectangle and lost the selection. I fixed that by giving a “blur” to the mouse up/down events. Is it that what you’re asking?

I know that you added blur to 1.1 but 1.1 still has the losing selection thing. However, that is not the problem here although it maybe related.

If you look at the code I posted it literally does only one thing. That is to get a destination point based on where the mouse is clicked down and print to the console. No selection or anything because its attached to a vehicle. This is failing to get a destination point 10% of the time.

My code is rather different then your but I am trying to focus on the getting destination problem cause that is the show stopper right now.

Its what I posted above is basically the same as
http://forum.unity3d.com/viewtopic.php?p=172982

public void Update() 
   { 
        if (Input.GetButtonDown("Fire1")) 
        { 
            
            Ray ray = Camera.allCameras[0].ScreenPointToRay(Input.mousePosition); 

            if (Physics.Raycast(ray, out hit, 10000)) 
            { 

                Debug.Log("Mouse Down Hit Terrain " + hit.point); 
                //your 'Goto' methods                
                Goto(hit.point); 
            } 
        } 
    }

I can post the whole Vehicle script if you want right now it’s totally self contained for testing purposes and uses a character controller for movement.

http://creators.xna.com/en-US/sample/picking

This is the info I have compiled so far. Do unity have something like Viewport.Unproject . To projects a vector from screen space (pixels) into object space? The way it being done now seems hack and incorrect!

Vector3 nearsource = new Vector3((float)mouseX, (float)mouseY, 0f);
Vector3 farsource = new Vector3((float)mouseX, (float)mouseY, 1f);

Matrix world = Matrix.CreateTranslation(0, 0, 0);

Vector3 nearPoint = GraphicsDevice.Viewport.Unproject(nearsource, 
    proj, view, world);

Vector3 farPoint = GraphicsDevice.Viewport.Unproject(farsource, 
    proj, view, world);

the above is xna code

I think this is an internal unity bug I cannot make sense of it. Maybe keep shooting rays until destination changes when called.

	private void Mouse1Down(Vector2 screenPosition) 
    {
		
	
		Vector3 where = new Vector3(screenPosition.x, screenPosition.y, 0);
		 
		
		RaycastHit hit;
		
        Ray ray = Camera.main.ScreenPointToRay(where);
     
		
        if(Physics.Raycast(ray, out hit, 10000, terrainLayerMask))
        {

            destination = hit.point;
			destination.x = (float)Math.Round(destination.x);
			destination.y = (float)Math.Round(destination.y);
			destination.z = (float)Math.Round(destination.z);
			Debug.Log (destination);
			
			moving = true;
           
        }
		
		else
		{
			Mouse1Down(screenPosition);
		}
       
    }

Above code does not seems to fix it and could lead to infinite recursion .

Both Input.GetButtonDown and Input.GetButtonUP are not getting set true all the time. Has anyone else ever had this? I am using a Logitech g9 on windows with Logitech drivers. For a while, the mouse wheel was not being sensed (in code but was working in editor)either but that went away any ideas? Updating the driver did not help.

when I switch from FixedUpdate()to Update ()the problem is solved. I was using fixed update for the character controller.