Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

percent of MousePos in window?

Discussion in 'Scripting' started by robertseadog, Nov 6, 2005.

  1. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    I made this script because the Input.mousePosition finds the mouseposition in the entire screen. I wanted my targetT to follow the mouse when in the widget window so I though of a way to multiply with the percentage of the total screen width/height. (sorry if this is a bad explanation :S ) Somehow though, my wonderfull creation fails to deliver what it should:
    Code (csharp):
    1.  
    2. var targetT : Transform;
    3. var targetG : GameObject;
    4. private var dist = 0;
    5. private var heiGht = 0;
    6. private var wiDth = 0;
    7. var layerMask = 1 << 8;
    8.  
    9. function FixedUpdate () {
    10. var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    11. var hit : RaycastHit;
    12. //also it doesn't seem to stop when the ray hits targetG?
    13.  
    14.     if (Physics.Raycast (ray, hit, 100, layerMask)) {
    15.     print ("The mouse hit the colliders, obj should stop");
    16.    // Input.ResetInputAxes(); // will this affect mousePos?
    17.     }
    18.     else {
    19.         //make the obj move after the windows mousePos:
    20.         targetT.position.x = Input.mousePosition.x * wiDth;
    21.         targetT.position.y = Input.mousePosition.y * heiGht;
    22.     }
    23. }
    24. function Start () {
    25.     if (!Screen.fullScreen) {
    26.     wiDth = 320 / Screen.width;
    27.     heiGht = 240 / Screen.height;
    28.     }
    29. targetG.layer = 8;
    30. }
    31.  
    when I try this the targetT wacks out completely. I haven't tried deploying it yet but this should work anyway no? Does this have anything to do with the way the screen.width/height is being measured?

    Any input is greatly appreciated here! :cry:
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Try this instead:

    targetT.position.x = Input.mousePosition.x / Screen.width;
    targetT.position.y = Input.mousePosition.y / Screen.height;
     
  3. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Wait a minute! This is what I want:

    Code (csharp):
    1.  
    2. (Input.mousePosition.x)*(Screen.width/320)*32;
    3. (Input.mousePosition.y)*(Screen.height/240)*24;
    4.  
    the only prob here is that the x value increases at the right corner of the screen.. It would be possible if the 0 value was dead center in stead. You know how to hack this?
     
  4. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    So what are you trying to accomplish? I am not following why you are multiplying the mouse position by 1/10 the screen size. If you just want to overlay a GUI Graphic where your mouse is, then Joachim's solution should work (it is what I have used). Are you instead wanting to give your mouse position as a percentage of where it is on the screen?
     
  5. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Like this?
    Code (csharp):
    1.  
    2. (Input.mousePosition.x/Screen.width)*2-1;
    3. (Input.mousePosition.y/Screen.height)*2-1;
    4.  
    That way (0,0) is the center of the screen, (-1,-1) is the bottom left and (1,1) top right.

    ... or if you want it as a percentace just multiply the lot with 100.
    Code (csharp):
    1.  
    2. (Input.mousePosition.x/Screen.width)*100;
    3. (Input.mousePosition.y/Screen.height)*100;
    4.  
    that will get you (50,50) as the center, (0,0) as the bottom left and (100,100) for top right.
     
  6. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    take a look at this : http://oddmagazine.com/testJR/testMove0.html

    the code used:
    Code (csharp):
    1. var w = 0.0;
    2. var h = 0.0;
    3. function Update () {
    4. w = ((Input.mousePosition.x/Screen.width)*2-1) * 320;
    5. h = ((Input.mousePosition.y/Screen.height)*2-1) * 240;
    6.     transform.position.x = w;
    7.     transform.position.y = h;
    8. }
    so Im getting closer (thanks Freyr!) but I need the obj to move to the point where the mouse is at..
     
  7. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Uh... that's just, like Joe said:
    Code (csharp):
    1.  
    2. targetT.position.x = Input.mousePosition.x / Screen.width;
    3. targetT.position.y = Input.mousePosition.y / Screen.height;
    4.  
    This will make a GUI object follow the mouse cursor exactly.

    Screen.width and height is not the resolution of the entire screen, but the size of the game view. Also, the mouse position is the position of the mouse within that game view. You don't need to do different things depending on whether the game is in full screen mode or not.
     
  8. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    hmm.. wierd cause when In debug the values are fullscreen.. I'll have to try to build it and see..

    EDIT: I can't get that to work sorry! I uploaded an example to show you the problem:

    http://oddmagazine.com/testJR/testMove.html

    This is using the following code:
    Code (csharp):
    1. var w = 0.0;
    2. var h = 0.0;
    3. function Update () {
    4. w = Input.mousePosition.x / Screen.width;
    5. h = Input.mousePosition.y / Screen.height;
    6.     transform.position.x = w;
    7.     transform.position.y = h;
    8. }
    as suggested.
     
  9. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Seems to me that perspective is the cause of your problems. If you move a box about in 3D, how far you need it to move is entirely dependant on the distance to the object.

    Your first code is actually closest to what you should do. But instead of trying to calculate x y from the screenwidth height, use the ray to calculate it:

    targetT.position = ray.origin + ray.direction * distance...

    Here, distance can be any value - depending on how far out you want the object to appear...
     
  10. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    I never knew 2d could be hard until I did it in 3d..
    http://oddmagazine.com/testJR/testMove0.html
    It worked pretty sweet Nicholas, but since your allready tuned in; you might have noticed that the obj is moving towards the camera at the edges, whats causing this?

    heres the final code
    Code (csharp):
    1. var target : Transform;
    2. var Cam : Transform;
    3. private var dist = 0;
    4. private var layerMask = 1 << 8;
    5. function FixedUpdate () {
    6. var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    7. var hit : RaycastHit;
    8.     if (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask)) {
    9.     //hitLayer = true;
    10.     print ("The mouse hit the colliders, obj stops");
    11.     Input.ResetInputAxes();
    12.     } else {  
    13.         target.position = ray.origin + (ray.direction * dist);
    14.     }
    15. }
    16. function OnDrawGizmos () {
    17.     Gizmos.color = Color.red;
    18.     Gizmos.DrawLine (transform.position, target.position);
    19. }
    20. function Start () {
    21. dist = (transform.position.z - Cam.position.z) * 1;
    22. }
     
  11. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    The thing that happens is that your ray is normalized (always has length=1)... This means that as it goes more out to the side of the screen, it will go less into the screen. Think of the ray describing an arc around your viewpoint, where what you want is a plane

    In order to get the behaviour you desire, you should divide the distance by ray.direction.z:

    target.position = ray.origin + (ray.direction * dist / ray.direction.z);
     
  12. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Thank you so much for all the patience and help! That did the trick Nicholas! ;)
     
  13. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    You're welcome.... ;)