- Home /
Rotating a GUI Object Based on Mouse Position with Locked Mouse
So, I've been working on this problem for a few days. I have a GUI object on screen that represents where the mouse position is. Not directly, but based on it's angle to the x-axis . I am able to calculate the angle decently well using Mathf.Atan2, however I'm having some issues. The cursor itself is locked to the center of the screen, so I've created variables mouseX and mouseY which are updated to track the mouse cursor using this code:
if (Input.GetAxis ("Mouse Y") > 0) {
mouseY += 1;
}
else if (Input.GetAxis ("Mouse Y") < 0) {
mouseY -= 1;
}
if (Input.GetAxis ("Mouse X") > 0) {
mouseX += 1;
}
else if (Input.GetAxis ("Mouse X") < 0) {
mouseX -= 1;
}
It works decently well. To fix the issue of the mouse getting lost in a quadrant I clamped both variables between -1 and 1. This works, except this causes the tracking to be confined in a square. What I would like to do is confine the mouse to a circle (the unit circle specifically.) While I know generally I could use the angle to do this, I'm attempting to find the angle, so I don't have it.
I initially tried to only record mouseY and then calculate mouseX using the formula for the unit circle. The issue with this, is it only allows rotations to occur in the first and fourth quadrant. I'm at a loss for how to do this and any advice would be appreciated. I didn't include additional code as all of its problems stem from this mouse issue.
@Slev, so basically, you want to draw a texture on the center of the screen, and it should always face the cursor?
@danilonishimura Yes and no. Basically I do want to do that, but I also want to be able to know the angle it is away from the x-axis. It's backend for something else I'm working on.
@Slev, yeah, so yes. If you rotated correctly the image, you know the angle. The reason your angle is always locked to 0 or 90 is because you're only considering the values like a integer matrix from -1,-1 to 1,1. Also sum$$anonymous$$g the values isn't the right way to do it. And one more thing, If you're getting the X values comparing it to "smaller than zero" or "greater than zero", basically means your mouse is to the left or right of the left part of the screen. (I'm going home right now and I'l post something more useful when I get there.)
Answer by danilonishimura · Jan 18, 2013 at 02:02 AM
So @Slev. I'm not quite sure about what's exactly the problem, so I created a simple code so you can see if this is what you're looking for. Create any C# class and replace the contents. If you're working with JS, it's a simple code to port in a few seconds.
public float angle;
public Texture2D arrow;
private bool initialized = false;
private GameObject plane;
void Start () {
plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
plane.transform.parent = Camera.mainCamera.transform;
plane.transform.localPosition = new Vector3(0,0,1);
plane.transform.localScale = Vector3.one * 0.02f;
plane.transform.eulerAngles = new Vector3(0,270,90);
StartCoroutine(LoadTexture());
}
IEnumerator LoadTexture()
{
WWW loader = new WWW("http://upload.wikimedia.org/wikipedia/en/7/72/Dark_Green_Arrow_Up.png");
while(!loader.isDone)
{
yield return new WaitForSeconds(0.5f);
}
arrow = new Texture2D(512,512,TextureFormat.PVRTC_RGBA4, false);
loader.LoadImageIntoTexture(arrow);
plane.renderer.material = new Material(Shader.Find("Transparent/Diffuse"));
plane.renderer.material.mainTexture = arrow;
initialized = true;
}
void FixedUpdate()
{
if(!initialized)
{
return;
}
Vector2 normalizedPositions = new Vector2((Input.mousePosition.x/Camera.mainCamera.GetScreenWidth()-0.5f), ((Input.mousePosition.y/Camera.mainCamera.GetScreenHeight())-0.5f));
angle = Mathf.Atan2(normalizedPositions.y, normalizedPositions.x)*Mathf.Rad2Deg;
plane.transform.eulerAngles = new Vector3(angle, 270,90);
}
@danilonishimura I appreciate the code, however I was able to find the solution. The problem was that I have a GUI element being rotated which used RotateAroundPivot. Eventually I figured out I could normalize the vector of the mouse position to lock it to within the unit circle.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091119im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Use OnMouseEnter/OnMouse Exit with center of screen 1 Answer
How to force refresh mouse position? 2 Answers
Ensuring Correct Call Order 0 Answers
Overlapping GUI Button priority 3 Answers