Nobert
Nobert

Reputation: 163

Convert from Canvas coordinates to In world coordinates

Every time the mouse is pressed, i log it's coordinates. What i need is a cube instantiated at the exact place the mouse was clicked.

I've looked through form posts and what i get is either answers witch are outdated or code that is really unreadable and messy. I tried using Camera.main.ScreenToWorldPoint with a Vector3 but the coordinates are always the same as the camera. Here is the line of code:

Instantiate(prefab, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,  Input.mousePosition.y, 0)), Quaternion.identity);

How do i convert from canvas coordinates to in world ones?

Upvotes: 0

Views: 717

Answers (1)

Iggy
Iggy

Reputation: 4888

You want to use the opposite function:

Camera.ScreenToWorldPoint(Vector3 position);

position: A screen space position (often mouse x, y), plus a z position for depth (for example, a camera clipping plane).

Upvotes: 4

Related Questions