Problem with getting mouse position

While i was going through the course, I am really confused with the conversion of mouse position into world unit :-

Block Breaker E__Unity projects_Block Breaker - ..._Assets_Scripts_PaddleMovement.cs - JetBrains Rider 30-12-2020 09_27_33 AM

What we r actually doing by dividing by Screen.width and then multiplying by 16, is conversion into world unit actually needed??
Plz help!!

Hi Abhinav,

Please refrain from posting screenshots of your code because it’s impossible to copy and paste lines from them. Instead, paste it here and format it properly.

Input.mousePosition.x/Screen.width normalises the screen. We get values ranging from 0 to 1. We multiply the value by 16 to get values ranging from 0 to 16.

The bottom left World Space coordinate in our camera viewport is (0, 0), our right edge is at x = 16.

Did this clear it up for you?


See also:

Sorry, but it is still a bit confusing…

i got the point that we get value from 0 to 1 and multiply it by 16 to get 0 to 16, but i didn’t understand why we need to divide it by width of screen and why can’t we use just the values we get by Input.mousePosition.x (i hope this format is correct, srry for the last tym). Basically, why r we doing all that multiplication and division stuff, we still get some values without that, aren’t those enough!!! :exploding_head: :pleading_face:

Maybe you should log the Input.mousePosition.x value into your console. Then run your game and move the mouse to the right edge of your game screen. Which value to you get? Stop your game and set the x-position of your paddle to the mouse position that you got at the right edge of your screen. Take a look at your game window? Are you still able to see the paddle?

In Unity we have three different spaces: screen space (where the mouse and the UI “live”), World Space (where components such as as MeshRenderer and SpriteRenderer “live”) and the camera viewport (which is normalised and could be used to determine the edges of the game screen during runtime). The three spaces are usually not compatible with one another. You’ll see that when you try to apply the mousePosition to the paddle position.

Okay…thnx
At last Something went inside my mind :slightly_smiling_face: :upside_down_face:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms