[ad_1]
In my sport i’ve a component which i would like person open and shut a gear with mouse, however I would like person do that in a circle on display screen, regardless of how large circle is I simply need to detect round mouse in clock-wise and counter-clockwise to extend and reduce a float worth.
How am i able to do this? I take advantage of unity.
Blender has one thing like this to twist vertices.
Note : I would like the answer particular for unity mouse enter system and with some instance code. Other matters are associated to multi contact and javascript.
EDIT 1 : This is my present code i developed utilizing javascript technique.
I’m unsure if it is working appropriately or not
utilizing UnityEngine;
public class Grabber : MonoBehaviour
{
// Public Values
public float sensivity = 0.01f;
public Canvas canvas;
// Internal Values
bool isDragging = false;
float finalAngle, presentAngle, worth = 0;
Vector2 finalMousePos, grabberPos, presentMousePos = Vector2.zero;
// Grabber Code
non-public void OnMouseDown()
{
isDragging = true;
}
non-public void Update()
{
if (isDragging)
{
// Draw Debug Line
grabberPos = Camera.most important.WorldToScreenLevel(this.remodel.place);
presentMousePos = Input.mousePosition;
if (presentMousePos != finalMousePos)
{
finalMousePos = presentMousePos;
// Calculate Angle
var dir = finalMousePos - grabberPos;
presentAngle = Mathf.Atan2(dir.x, dir.y);
// Apply Value Changes
if (finalAngle != presentAngle)
{
if (presentAngle > finalAngle) worth += sensivity;
if (presentAngle < finalAngle) worth -= sensivity;
finalAngle = presentAngle;
}
}
if (Input.GetMouseButtonUp(0)) isDragging = false;
ScreenGizmos.DrawLine(canvas, Camera.most important, grabberPos, finalMousePos, Color.crimson);
// Apply Value
this.remodel.localScale = new Vector3(1 + worth, 1 + worth, 1 + worth);
}
}
non-public void OnGUI()
{
if (!isDragging) return;
GUI.pores and skin.label.fontSize = 20;
GUI.pores and skin.label.richText = true;
var textual content = $"<shade=inexperienced>Current Angle : {finalAngle:F4}</shade>nGrabber Pos {grabberPos.x} {grabberPos.y}nCursor Pos {finalMousePos.x} {finalMousePos.y}";
textual content += $"n<shade=yellow>Value : {worth}</shade>";
var place = finalMousePos;
var textSize = GUI.pores and skin.label.CalcSize(new GUIContent(textual content));
GUI.Label(new Rect((place.x) + 10, (Screen.top - place.y) - 10, textSize.x, textSize.y), textual content);
}
public static float Cross2D(Vector2 value1, Vector2 value2)
{
return value1.y * value2.y - value1.x * value2.x;
}
}
public static class ScreenGizmos
{
non-public const float offset = 0.001f;
public static void DrawLine(Canvas canvas, Camera digital camera, Vector3 startPixelPos, Vector3 endPixelPos, Color shade)
canvas == null)
return;
Vector3 startWorld = PixelToCameraClipPlane(
digital camera,
canvas,
startPixelPos);
Vector3 endWorld = PixelToCameraClipPlane(
digital camera,
canvas,
endPixelPos);
Debug.DrawLine(startWorld, endWorld, shade);
non-public static Vector3 PixelToCameraClipPlane(Camera digital camera, Canvas canvas, Vector3 display screenPos)
{
display screenPos *= canvas.scaleFactor;
display screenPos.z = digital camera.nearClipPlane + offset;
return digital camera.ScreenToWorldPoint(display screenPos);
}
}
Result :
[ad_2]
