- Home /
Physics-based controls and slopes ?
Hi, I'm currently making a 3D game in which the player can control a fish, which is achieved through rigidbody operations. My current controls are quite good to me, except I had to uncheck "Freeze Rotation" on the Y Axis to perform rotations using AddTorque. Consequently, every time I try to climb a slope, my fish begins to turn perpendicular to it (which is how "normal" physics is supposed to happen when you use a capsule collider I guess). So I'm currently asking if you knew a mean to either :
1 - Make AddTorque override Freeze Rotation (this way I could just re-check Freeze Rotation in Y and it would be perfect).
2 - Make my rigidbody ignore the physics that make my fish go perpendicular, one way or another.
3 - If nothing else is doable, implement a simple solution to do rotations using anything else than AddTorque for a kind of similar effect (drag control).
Of course, I'm also open to any kind of alternative / better way to solve my problem :)
Thanks for your time.
Some clarifications, please:
A diagram would really help understanding (please!).
2D / 3D ?
You say "had to uncheck \"freeze Rotation\"" but it's not checked by default (AFAI$$anonymous$$). Are you working from an existing project / tutorial or something? (It'd help establish the picture.)
When you said "fish", I envisage "floating freely in the depth of a body of water" (i.e. under-water). Not classically a situation requiring a lot of slope following (?!). Perhaps you could include some detail about viewpoint and what movement is allowed/expected, please?
I'll pop some notes on /a/ potential approach as an answer below in case it helps but I fear I'm on the wrong lines (hence the clarifications).
Answer by Barachiel · Aug 26, 2015 at 07:01 PM
With my own Rigidbody controller, I zero out the rotations in script and use either Mathf.SmoothDamp or Vector3.SmoothDamp as needed for smooth rotations.
That helped a bit. Used $$anonymous$$athf.SmoothDamp and it worked pretty fine (thanks), then added a "constant force" variable which gets up or down according to the player's input AND closer to zero at every frame (whatever the input is). The constant force is then added to the actual rotation. The effect is pretty close to what I was trying to achieve. Thanks guys :)
Answer by Arkade · Aug 23, 2015 at 09:10 PM
(following-on from my clarification request comment above)
Assuming 3D, I'd probably start by modelling the fish as having heading vector which you modify with your controls. I'd then orient the fish with [Transform.LookAt(Vector3 worldPosition)].
I'm gonna pause there for now. Given my uncertainty about the question, I hesitate to take more time to give more detail until a little confirmation that this is in the right direction (e.g. not 2D top view or something!)
HTH?
[1]http://docs.unity3d.com/ScriptReference/Transform.LookAt.html
Thanks for the response ! (i'll just answer both of your messages right there)
I fixed the question : it's a 3D game ! (I posted an in-game screenshot below)
1.
I had to uncheck it in order to perform AddTorque rotations, elseway it would just not rotate. I'm not working on an existing project / tutorial.You say "had to uncheck \"freeze Rotation\"" but it's not checked by default (AFAI$$anonymous$$). Are you working from an existing project / tutorial or something? (It'd help establish the picture.)
2.
The thing is, my fish stays underwater thanks to a capsule collider attached to it : since I don't need it to move on the Y axis (in my game it cannot swim towards the bottom nor the top of the water), I just used a collider + gravity so he could follow my terrain since it it's swim$$anonymous$$g in a river with bumps and slopes. I know it may seem dirty but I'm ok with it from now.When you said "fish", I envisage "floating freely in the depth of a body of water" (i.e. under-water). Not classically a situation requiring a lot of slope following (?!). Perhaps you could include some detail about viewpoint and what movement is allowed/expected, please?
Here is what it looks like in editor : http://i.imgur.com/84aeNJS.png
And here's an in-game view : http://i.imgur.com/wHjzB8u.png
3.
Hope I did answer some of your questions about the game, if you still need some details please tell me what the diagram should be for, I would do it right away.A diagram would really help understanding (please!).
The LookAt solution is good but I do need the fish to rotate in a more "natural-like" fashion (e.g rotation slowing down gently when no input is made from the player). That's why I chose to use AddTorque in the first place.
Still, there may be a way to use LookAt ins$$anonymous$$d of AddTorque, with the Vector3.x variable going up / down in every Update to match the fish's Forward.x, besides getting also increased or decreased according to the player's input. I must admit I didn't try that one as I was seeking a way to use AddTorque exclusively (that's mainly because I'm quite lazy with code actually). I also felt it would not feel as "natural" as the built-in physics system, but that's pure speculation anyways. If no other solution is given, I'll pick that one for sure :)
Again, thanks for your time.
Your answer
Follow this Question
Related Questions
How do I nullify the torque from colliding but not from player input? (2D,C#) 1 Answer
How can I stabilize a vehicle's roll during a jump using forces? 1 Answer
Compute the torque needed to rotate an object at a constant rate 3 Answers
Returning a rigidbody back to its original x and z rotations through physics forces. 2 Answers
Physic Torque Hinge joint problem 0 Answers