- Home /
Question by
Kajen_117 · Oct 07, 2021 at 09:05 AM ·
2dscripting problem2d game
Trying to get gameobject to point to mouse in a orbit around the player 2D
Basically I have the object rotating around the player but am not able to get it to actually point to the mouse. Instead it just spins rapidly around the player. I feel like my math setup should be correct but I just need help identifying why it isn't working.
Vector3 origin = cam.WorldToScreenPoint(transform.position);
origin = (Input.mousePosition - origin);
float angle = Mathf.Atan2(origin.y, origin.x) * Mathf.Rad2Deg; //arctan(origin.y/origin.x)
float pointer_x = (radius * (float)Mathf.Cos(angle)) + transform.position.x; //x = r * cos(theta)
float pointer_y = (radius * (float)Mathf.Sin(angle)) + transform.position.y; //y = r * sin(theta)
pointer.transform.position = new Vector2(pointer_x, pointer_y);
Comment
Answer by logicandchaos · Oct 08, 2021 at 01:40 PM
Try this: //make your origin vector Vector3 origin = cam.WorldToScreenPoint(transform.position); origin = (Input.mousePosition - origin);
//normalize it
origin = origin.normalized;
//make it the right distance away
float distance = 10;//adjust to value you need
origin *= distance;