- Home /
Simulating a Fidget Spinner
Hi, I just started some code that is supposed to simulate a real world fidget spinner. After scribbling the system on paper i thought i had a good solution, and it works fine for some angles except around certain angles where it starts to show some weird behaviour sort of changing direction. I can't figure out what's causing this. Pasting code below.
if (Input.GetMouseButton(0))
{
Debug.Log(transform.rotation.z.ToString());
Vector3 v_deltaInput = lastInput - Input.mousePosition;
Vector3 v_unitTheta = -Mathf.Sin(transform.rotation.z) * Vector3.right + Mathf.Cos(transform.rotation.z) * Vector3.up;
f_projection = Vector3.Dot(v_unitTheta, v_deltaInput);
transform.Rotate(Vector3.fwd, f_projection);
lastInput = Input.mousePosition;
}
else {
transform.Rotate(Vector3.fwd, f_projection);
}
}
Answer by UnnamedGameDev · Jan 24, 2018 at 05:07 PM
Solved. Perhaps abit hard-coded, but it works in my specific casae. I'll post my solution here incase anyone else runs in to a similiar problem. Basically u pop it on a 2d sprite and spin.
if (Input.GetMouseButtonDown(0))
{
f_projection = 0.0f;
}
if (Input.GetMouseButton(0))
{
Vector3 v_deltaInput = lastInput - Input.mousePosition;
Vector3 v_unitX = new Vector3(1, 0, 0);
Vector3 v_unitY = new Vector3(0, 1, 0);
f_projectionX = Vector3.Dot(v_unitX, v_deltaInput);
f_projectionY = Vector3.Dot(v_unitY, v_deltaInput);
if (Input.mousePosition.y < Screen.height / 2)
{
f_projectionX = -f_projectionX;
}
if (Input.mousePosition.x > Screen.width / 2)
{
f_projectionY = -f_projectionY;
}
f_rot = f_projectionX + f_projectionY;
transform.Rotate(Vector3.forward, f_rot);
lastInput = Input.mousePosition;
}
else
{
transform.Rotate(Vector3.forward, f_rot);
}
Your answer
Follow this Question
Related Questions
Rotating Object on its Local World Axis 4 Answers
eulerAngles/rotation halts abruptly 1 Answer
Instantiating an object in front of the player 3 Answers
Continuously rotating by 90 doesn't get me the initial angle 0 Answers
Transition between rotations 0 Answers