playMaker

Author Topic: Get Mouse X and Get Mouse Y Problem![SOLVED]  (Read 3691 times)

trashtrash

  • Playmaker Newbie
  • *
  • Posts: 3
Get Mouse X and Get Mouse Y Problem![SOLVED]
« on: January 27, 2013, 11:41:30 AM »
     Hi I am only three weeks into using your product and still fairly new to unity 3d so I apologize if this is a dumb question. I have a FSM which has get mouse x and get mouse y actions on it for every frame at normalized coordinates. X and Y are stored as a float variable. On the same state below the x and y actions I have a get position action and the x and y variable of the mouse x and y are used, the z is set to 0.This FSM is placed on a empty game object that is used to spawn a prefab. The goal is to have the spawn point x and y move accurately with the mouse x and y. It is moving a little bit but not accurately at all. It's like it's clamped at -1 to 1 it moves with the mouse but not where the mouse actually is. I am starting to suspect this is a unity 3d problem or something but If any of you could help it would be greatly appreciated. Also I played with setting it to world instead of self and then it gave me values that where way off the screen. Thanks in advance for your help!
« Last Edit: January 28, 2013, 01:02:17 AM by jeanfabre »

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: Get Mouse X and Get Mouse Y Problem!
« Reply #1 on: January 27, 2013, 12:36:47 PM »
Hey and welcome,
The mouse is in a 2D (camera) system which is not at all related to any 3D space in the scene. This system describes the mouse position on the screen between (0,0)(upper left corner) and (1,1)(lower right corner).

Luckily for us unity has an integrated function to turn this into a screen point by using a raycast. Normally since the camera is normally perspective you would not be able to just draw a straight line from the screen outwards and hit the right gameObject in the scene.

That being said you can do a "screen pick" from your screen coordinates or just use a "mouse pick", which always uses the mouse as the start of the ray.
Then you save to hit point and set the position of your cube to that hit point. If you do this every frame, the cube will follow your mouse while being stuck on the ground.

Note: If your cube has a collider it instead of the floor would be hit by the raycast. So make sure to put it into a new layer and then use an inverted layer mask on your pick action. That will make it ignore everything in that layer. 
Best,
Sven

trashtrash

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Get Mouse X and Get Mouse Y Problem!
« Reply #2 on: January 27, 2013, 09:05:58 PM »
Thanks for info I figured it out!