- Home /
Limit Mouse Look to Circle
Prefacing: I'm creating a 2.5d Side Scroller. I'm beginning to program a cursor. It's a custom cursor (I hide the cursor using Screen.showCursor = false). I'm trying very hard to limit the cursor to a small radius around the player.
Problem: Well, obviously, something isn't working. I've tried using the pseudocode here: http://answers.unity3d.com/questions/42686/keep-object-inside-circle.html and that got me much further than I would have on my own but I'm running into some problems. When I set the radius of the circle absurdly high (5, or so) the "distance" returns to be between -1 and 1. Which makes sense, since it's a % across the screen. However, when I change to be .2 then all of the sudden the distance is .9 or higher. (and my cursor seems to jump to up to "13 screens" away when the distance is 13+)
#pragma strict
var regCursor : Texture2D;
var diff : Vector2;
var parent : GameObject;
var circleRadius : float = 5;
function Start(){
Screen.showCursor = false;
transform.localScale = Vector3(.03,.03,.03);
guiTexture.texture = regCursor;
}
function Update(){
var mousePos = Input.mousePosition;
MoveMouse(mousePos.x/Screen.width, mousePos.y/Screen.height);
}
function MoveMouse(mousePosX : float, mousePosY : float) {
var location = transform.position;
var parentLocation = Camera.main.WorldToViewportPoint(parent.transform.position);
//Actually Move our Software Mouse
var parentLoc = new Vector2(parentLocation.x, parentLocation.y);
diff = (location - parentLoc);
var distance = (location - parentLoc).magnitude;
Debug.DrawLine(transform.position, parent.transform.position, Color.red, 10.0, true);
print("dist is "+distance+" circleRadius is "+circleRadius+ ": "+distance+">"+circleRadius);
if(distance > circleRadius){
transform.position = parent.transform.position + (diff / distance) * circleRadius;
}
else{
transform.position.x = mousePosX;//these two lines work alone
transform.position.y = mousePosY;
}
}
The Debug line starts and stays in the same spot. Edit: It does point to somewhere based on where the player is standing and I can't figure out why. For some reason, when I run all the way right, it points up left. Any insight here?
Edit: It's been answered, and here's the result:
var regCursor : Texture2D;
var diff : Vector2;
var parent : GameObject;
var circleRadius : float = 5;
var mousePos : Vector3;
function Start(){
Screen.showCursor = false;
transform.localScale = Vector3(.03,.03,.03);
guiTexture.texture = regCursor;
}
function Update(){
mousePos = Input.mousePosition;
MoveMouse(mousePos.x/Screen.width, mousePos.y/Screen.height);
}
function MoveMouse(mousePosX : float, mousePosY : float) {
var location = Camera.main.ScreenToViewportPoint(mousePos);
var parentLocation = Camera.main.WorldToViewportPoint(parent.transform.position);
var parentLoc = new Vector3(parentLocation.x,parentLocation.y,0);
//Actually Move our Software Mouse
diff = (location - parentLoc);
var distance = (location - parentLoc).magnitude;
if(distance > circleRadius)
{
transform.position.x = (parentLoc.x +(diff.x/distance) *circleRadius);
transform.position.y = (parentLoc.y +(diff.y/distance) *circleRadius);
}
else
{
transform.position.x = mousePosX;
transform.position.y = mousePosY;
}
}
Answer by whydoidoit · Jun 14, 2012 at 05:45 PM
Well you are converting parentLocation to viewport coordinates but location is still in world coordinates - that's got to be the root of your problem.
I actually messed up in a lot of ways. But, your answer lead me to what I needed to see ins$$anonymous$$d of giving me what I wanted to read. I'm updating my post with the my final code.
Your answer
Follow this Question
Related Questions
Drawing Sector around GameObject in GameView: GUITexture or 3D Object 1 Answer
Rotating around a point in a circular motion depending on mouse position 1 Answer
Find distance around a circle, from center point and another point 2 Answers
Getting radius around multiple transform 1 Answer
Rotate around a circle 0 Answers