How can I implement snapping via code? I have this 2d circle that the players rotate via drag and it works like a rotating dial. The best way to describe what I wanted to achieve is similar to a clock hand snapping to the hour numbers. I'm a total beginner and haven't actually learned anything about quaternions.
[![Image of dial with 0 at the top and 7 increment marks evenly spaced around the rest][1]][1]
[1]: https://i.sstatic.net/E7wvd.png
This is the my code , im using a raycast to detect if the dial is clicked/dragged.
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector3.forward);
if (Input.GetMouseButtonDown(0))
{
if (hit.collider && hit.collider.GetComponent<Rotate2d>())
{
deltaRotation = 0f;
previousRotation = angleBetweenPoints(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
}
else if (Input.GetMouseButton(0))
{
if (hit.collider && hit.collider.GetComponent<Rotate2d>())
{
currentRotation = angleBetweenPoints(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
deltaRotation = Mathf.DeltaAngle(currentRotation, previousRotation);
if (Mathf.Abs(deltaRotation) > deltaLimit)
{
deltaRotation = deltaLimit * Mathf.Sign(deltaRotation);
}
previousRotation = currentRotation;
hit.collider.gameObject.transform.Rotate(Vector3.back * Time.deltaTime, deltaRotation);
}
}