percent of MousePos in window?

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:

var targetT : Transform;
var targetG : GameObject;
private var dist = 0;
private var heiGht = 0;
private var wiDth = 0;
var layerMask = 1 << 8;

function FixedUpdate () {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
//also it doesn't seem to stop when the ray hits targetG?

    if (Physics.Raycast (ray, hit, 100, layerMask)) {
    print ("The mouse hit the colliders, obj should stop");
   // Input.ResetInputAxes(); // will this affect mousePos?
    }
    else {
        //make the obj move after the windows mousePos:
        targetT.position.x = Input.mousePosition.x * wiDth;
        targetT.position.y = Input.mousePosition.y * heiGht;
    }
}
function Start () {
    if (!Screen.fullScreen) {
    wiDth = 320 / Screen.width;
    heiGht = 240 / Screen.height;
    }
targetG.layer = 8;
}

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:

Try this instead:

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

Wait a minute! This is what I want:

(Input.mousePosition.x)*(Screen.width/320)*32;
(Input.mousePosition.y)*(Screen.height/240)*24;

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?

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?

Like this?

(Input.mousePosition.x/Screen.width)*2-1;
(Input.mousePosition.y/Screen.height)*2-1;

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.

(Input.mousePosition.x/Screen.width)*100;
(Input.mousePosition.y/Screen.height)*100;

that will get you (50,50) as the center, (0,0) as the bottom left and (100,100) for top right.

take a look at this : http://oddmagazine.com/testJR/testMove0.html

the code used:

var w = 0.0;
var h = 0.0;
function Update () {
w = ((Input.mousePosition.x/Screen.width)*2-1) * 320; 
h = ((Input.mousePosition.y/Screen.height)*2-1) * 240;
    transform.position.x = w;
    transform.position.y = h;
}

so Im getting closer (thanks Freyr!) but I need the obj to move to the point where the mouse is at…

Uh… that’s just, like Joe said:

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

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.

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:

var w = 0.0;
var h = 0.0;
function Update () {
w = Input.mousePosition.x / Screen.width; 
h = Input.mousePosition.y / Screen.height;
    transform.position.x = w;
    transform.position.y = h;
}

as suggested.

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…

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

var target : Transform;
var Cam : Transform;
private var dist = 0;
private var layerMask = 1 << 8;
function FixedUpdate () {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
    if (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask)) {
    //hitLayer = true;
    print ("The mouse hit the colliders, obj stops");
    Input.ResetInputAxes();
    } else {  
        target.position = ray.origin + (ray.direction * dist);
    }
}
function OnDrawGizmos () {
    Gizmos.color = Color.red;
    Gizmos.DrawLine (transform.position, target.position);
}
function Start () {
dist = (transform.position.z - Cam.position.z) * 1;
}

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);

Thank you so much for all the patience and help! That did the trick Nicholas! :wink:

You’re welcome… :wink: