- Home /
Rotating sphere with rigidbody attached
Hello! I have a sphere with rigidbody and sphere collider attached. I want it to move forward/backward using vertical input and to rotate along Y-axis using horizontal input. But there arises 2 problems:
1) When the sphere moves, its axes change (in local scope) and sometimes it rolls aside instead of rotating along the y-axis.
Maybe I have to first set sphere's rotation to zero and only then rotate it? I've tried transform.Rotate and rigidbody.rotation, both methods worked, but they were unnatural and way too fast. While I want my sphere to be affected by physics forces.
If there's a way to smoothly rotate an object form the any current rotation to "0, horizontal input, 0", please tell me about it :)
2) After the rotation is done I want my sphere to move forward/backward according to its current rotation (local vectors' direction). Is the right way to do that is using Rigidbody.AddRelativeForce() ?
I'm new to Unity, therefore any help will be appreciated!
P.S.: sorry for possible grammar mistakes.
You have basically two options when moving objects. You either use forces and allow the object to do what those forces cause it to do (which means you won't have such strict control over the object as to keep it's rotation aligned), or you ignore forces and move the object by altering it's position and rotation - but this means you're ignoring all physics (basically).
Combining the two concepts is going to lead to nightmare level headaches, because the physics engine is not designed to allow for it (even though you can perform the act in code, you'll cause very strange results, sometimes puzzling, sometimes hilarious).
One advantage to using forces is that you're going to get fairly smooth motion (all factors being in control toward that result, which is to say NOT punching a sphere with the equivalent force of a jet engine).
When you move something by script through an object's transform, you take responsibility for time. That is, you're going to have to consider each and every frame, and the small, sequential bits of motion required for each frame (or fixed timestep) in order to accomplish smooth motion.
Say, for example, you wanted a ball to spin on the Y axis (we're talking clockwise looking down upon the ball from above, right?), but only from script, not by adding a torque. Let's say you wanted it to spin at 60 RP$$anonymous$$, which is 1 revolution per second. Let's say you decide to used the FixedUpdate firing 60 times per second. You'll need to rotate the object's localRotation in Y by 6 degrees each time step. (Note, you can do it in Update, but that's not fixed, and can vary from frame to frame, depending on what else is going on - meaning that wouldn't be 6 degrees each frame, it would vary according to time). In either case, you'll use Time.deltaTime to know how much time has elapsed since the last update (or fixed update).
Thanks for your answer! Very sad, that the physics engine doesn't allow for so much control over the object... I'll try to do it from script (but it will take a few days,definitely). Then I'll update my question.
There's a third solution: Every object has an angular velocity. $$anonymous$$aking use of AngleAxis $$anonymous$$athf.RadToDeg and your input will give you full control over the rotation of a physics object when applied in FixedUpdate.
Sorry, in that tutorial the ball only moves forward/backward and aside, there is no rotation.. But thanks for answer anyway:)
Answer by tiredamage42 · Jul 07, 2018 at 06:19 PM
Use rigidbody.AddRelativeTorque(0,horizontalinput,0) for the rotation. And then rigidbody.AddRelativeForce() for the translation
Unfortunately, the solution isn't that simple :( I used these funtions first of all. Anyway, thank you for the answer!