How do I get my aim cursor to stay near the border of the circle?

10 hours ago 3
ARTICLE AD BOX

With this code I was able to get my character to move and aim a reticle around with an Xbox controller using Unity's new input system. It will also reorient the reticle to face outward from the center of the character which is desired. My question though is how do I get it to clamp to the outer edge of the circle. So for example if my stick is due right and only halfway, it would still be at the furthest right position as if you had the stick moved all the way. I also think I may be a little confused about the coordinates and am mixing them. When I was testing the game the 0 seemed to be north and 180 south. The game's coordinates aren't that way and I know that the thumbstick's inputs are -1 to 1. I am out of practice with my vector math and I can feel it. I would also welcome any advice on how to improve this code and maybe how to understand more deeply what's going on just so I can replicate this again in the future. Thank you.

void FixedUpdate() { rigidBody.linearVelocity = new Vector2(moveDirection.x * moveSpeed, rigidBody.linearVelocity.y); if (isJumping && isGrounded) { rigidBody.AddForce(Vector2.up * jumpHeight); isGrounded = false; Debug.Log("I Jumped"); } float angle = 0.0f; // Top Right Quadrant if (aimDirection.x >= 0.0f && aimDirection.y > 0.0f) { angle = Mathf.Atan(aimDirection.y / aimDirection.x) * 180 / Mathf.PI; } // Top Left Quadrant else if (aimDirection.x < 0.0f && aimDirection.y >= 0.0f) { angle = Mathf.Atan(aimDirection.y / aimDirection.x) * 180 / Mathf.PI + 180; } // Bottom Right Quadrant else if (aimDirection.x <= 0.0f && aimDirection.y < 0.0f) { angle = Mathf.Atan(aimDirection.y / aimDirection.x) * 180 / Mathf.PI - 180; } // Bottom Left Quadrant else if (aimDirection.x > 0.0f && aimDirection.y <= 0.0f) { angle = Mathf.Atan(aimDirection.y / aimDirection.x) * 180 / Mathf.PI; } AimingReticle.rotation = Quaternion.Euler(0f, 0f, angle); previousAimDirection = aimDirection; AimingReticle.position = new Vector2(aimDirection.x + transform.position.x, aimDirection.y + transform.position.y); }
Read Entire Article