2
\$\begingroup\$

I'm trying to create a game that we have a falling ball and an spinning object that spin based on user mouse position. user should rotate the object and hit the ball on time to throw the ball into sky. The problem is that if user move his finger too fast. the game object moves too fast and goes past through the ball. This is my code so far:

 void Update () {
         if (Input.GetMouseButtonDown(0)) {
             startPos = Input.mousePosition.x;
             startRot = obj.transform.localEulerAngles;
             lastDist = 0;
         }

         if (Input.GetMouseButton(0)) {
             float dist = Input.mousePosition.x - startPos;

             obj.MoveRotation(Quaternion.Euler(startRot + new Vector3(0, 0, -dist)));

             for (int i = 0; i < obj.transform.childCount; i++)
                 obj.transform.GetChild(i).GetComponent<Rigidbody>().MoveRotation(Quaternion.Euler(startRot + new Vector3(0, 0, -dist)));
         }
 }

How can I achieve this behavior for my game. The user should be able to spin the game object as fast as possible.

Thanks in advance.

\$\endgroup\$
  • \$\begingroup\$ Maybe try adding Torque to it? \$\endgroup\$ – John Hamilton Dec 11 '17 at 10:19
  • \$\begingroup\$ @JohnHamilton Yes. but if I use toque for this purpose. It will rotate on a base speed. I wanna control rotation with mouse drag. Any idea for this? \$\endgroup\$ – Hossein Maktoobian Dec 11 '17 at 10:22
  • \$\begingroup\$ What do you mean when you say "on a base speed", you can add torque as much as you want and as fast as you want. You can just register the finger movement then translate that into one big torque at the end of the movement or just add it over time while the player is swiping their finger. This all depends on how you design the game around this functionality. \$\endgroup\$ – John Hamilton Dec 11 '17 at 10:26
  • \$\begingroup\$ I mean. that if I add a torque to it. The object will begin rotating and not stops until it hits sth or get a negative torque. The most important thing is to control the rotation with mouse. If you know how? please submit an answer to this question \$\endgroup\$ – Hossein Maktoobian Dec 11 '17 at 10:51
  • \$\begingroup\$ If the user holds the mouse down for 1 second in the same place, you could just detect that and stop the object. Like I said, it really really just depends on the implementation and game design. \$\endgroup\$ – John Hamilton Dec 11 '17 at 11:21
1
\$\begingroup\$

These kinds of issues arise when the speed of the interaction is faster than the resolution of the Physics engine. Essentially the object has passed completely through the other object in the moments between when the physics engine was paying attention to them.

Increase the Physics run rate in the Physics settings.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.