Skip to content

Instantly share code, notes, and snippets.

@JuniorDjjr
Last active January 5, 2024 17:25
Show Gist options
  • Save JuniorDjjr/5df7f56f9e9abc06d15fcc3655bad7f1 to your computer and use it in GitHub Desktop.
Save JuniorDjjr/5df7f56f9e9abc06d15fcc3655bad7f1 to your computer and use it in GitHub Desktop.
Unity C# weapon wheel selector better than GTA V https://www.mixmods.com.br/2022/05/jogo-impunes-achou-que-eu-tava-brincando/
// Snippet by Junior_Djjr, originally for IMPUNES game. MIT license.
// This solution is better than GTA V when selecting diagonal slots on mouse. With only 10 lines of code.
// Only for wheel selector based on 8 slots, but you can easily change it.
// Mouse only for now
cursorPos.x += Input.GetAxis("Mouse X") * Time.unscaledDeltaTime * sensibility;
cursorPos.y += Input.GetAxis("Mouse Y") * Time.unscaledDeltaTime * sensibility;
float distFromCenter = cursorPos.magnitude;
// Limit cursor in a circle
if (distFromCenter > 100.0f)
{
cursorPos = cursorPos.normalized * 100.0f;
}
// Only select new weapon if cursor is a little off center (to be able to cancel)
if (distFromCenter > 30.0f)
{
// Convert cursor position to 0-360 angle
float cursorAngle = Vector2.SignedAngle(Vector2.down, cursorPos) - 180.0f;
// Get 0.0-1.0 angle with half slot offset
float cursorAngleNormalized = (cursorAngle - (45.0f / 2.0f)) / 360.0f;
// Convert to 0-8 int
int slotSelected = Mathf.Abs((int)(cursorAngleNormalized * 8.0f));
// 8 and 0 are same
if (slotSelected == 8) slotSelected = 0;
Debug.Log(slotSelected);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment