Mouse click to world space

Trying to return the mouse click as a value for world space.

I understand that the mouse coordinate system is different then world system so I need to us a screenToWorldPoint.

But the vector3 it returns is always my camera transform.position no matter where I click on the screen…


pointMouseVector =  camera.ScreenToWorldPoint(Input.mousePosition);


C# is the language I am working in…

put this as a global in your class
public float distance = 50f;

    //replace Update method in your class with this one
	void FixedUpdate () 
        //if mouse button (left hand side) pressed instantiate a raycast
             //create a ray cast and set it to the mouses cursor position in game
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast (ray, out hit, distance)) 
                //draw invisible ray cast/vector
                Debug.DrawLine (ray.origin, hit.point);
                //log hit area to the console

I just tested and it works fine.
It is in c#, I am hoping you can guess that though.
create a new project, create a new scene, add a fps char controller (import asset), create a new script(call it Raycast or something), add the above code in to it(removing Start & Update functions, as stated in code), pop the new script on to your fps controller (add it to the “main camera” in FPS controller).
Create a plain, pop in your FPS to the scene above the plain and play game.
Use the left mouse click to cast your ray.
Look at the CONSOLE to see the vector positons of the cast into the world space, you now have access to these in the code as (hit.point), use this as an argument to place something or do something in that place if oyu need more than juts a functional world space identifier.
Hope that helps some.
Cheers. Gruffy :slight_smile: