• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by UnnamedGameDev · Jan 24, 2018 at 01:30 PM · rotationobjecteuleranglesanglesspin

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);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
         }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

104 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My Obstacle Rotations Are Not Correct 0 Answers

How can I store the individual values of EulerAngles on a Database then re-apply 2 Answers

Problem when checking a Quaternion Eulerangle axis 1 Answer

Euler Angles question 1 Answer

How to get inspector rotation values ? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges