- Home /
How to set a player's gravity relative to a mobius strip?
So I'm creating an experimental game in which the player may travel endlessly on a mobius strip. I got it all rendered in Blender, threw it into Unity, but then I realized a new problem: I'm not sure how to get gravity to function for this thing.
Anyone got any tips or scripts that I could use to keep a player grounded to the thing without having to climb up or fall down any curves?
this question seems to come up all the time. Really someone should make a good "surface sticker" !
Here for example is one of many ludicrously long arguments about it, @dre
http://answers.unity3d.com/questions/545844/make-player-character-stick-to-the-level-mesh-and.html
Answer by Marsupilami · Oct 11, 2013 at 11:18 AM
Turn off gravity on the player's rigidbody.
Raycast down from player to get the normal of triangle hit and align player to that normal. Ensure the surface of your Mobius does NOT have triangles that are too big. You want them to be on average about the size of 1 Unity unit (1 meter) or less.
RaycastHit hit;
if (Physics.Raycast(transform.position, -transform.up, out hit)) {
transform.rotation = Quaternion.LookRotation(Vector3.Cross(transform.right, hit.transform), hit.transform);
}
Apply a downforce relative to players transform. Make gravitySpeed public and adjust accordingly.
rigidbody.AddForce(-transform.up * Time.deltaTime * gravitySpeed);
"Raycast down" how ?
You can't .. it's an AI question at best.
there's a huge discussion about it here
http://answers.unity3d.com/questions/545844/make-player-character-stick-to-the-level-mesh-and.html
the scheme you describe is how normal say car race titles (on relatively flat, sedate, surfaces) work
Raycast down from the current rotation. This can be improved by adding additional raycast at various angles and using the result with the least distance to deter$$anonymous$$e the closest mesh triangle.
You could even use the 2 smallest distances and combine the 2 to create a smooth transition for sharper angles.
It all comes down to creating a solution that fits the situation. I think a relatively large $$anonymous$$obius with sufficiently populated mesh can be done with 1 raycast. Using 13 raycasts, I believe you could attach to even the most complex mesh.
If you want a real fully coded example ins$$anonymous$$d of learning and experimenting, drop me the $$anonymous$$obius model and I'll whip up an example.
I was able to do this $$anonymous$$agic Boots with $$anonymous$$imal coding and only 3 spherecast. It even has jumping. Not too far stretch to polish this to work on just about any surface and have it able to drop into any character and work.
Was able to preserve the same integrity of the above video using a single spherecast.
RaycastHit hit;
if (Physics.SphereCast(transform.position, 0.5f, -transform.up, out hit, 5))
{
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hit.normal), hit.normal), Time.deltaTime * 5.0f);
}
rigidbody.AddForce(-transform.up * Time.deltaTime * gravitySpeed);
Answer by DaveA · Oct 10, 2013 at 09:28 AM
Instead of attracting the player to the strip, can you rotate the entire world/strip under the player? Then it would be finding the normal of the poly you want your feet on, and aligning the strip's Up axis with that.
I thought about that, but I've got the extra issue of being fairly new to Unity, and I'm still building my coding knowledge and ability at this point.
Do you think that the script would be a bit too complicated to figure out at this point? I looked through the thread Fattie sent me, but most of it seemed to be conjecture and arguments as to what would work best. There was an explanation of how it could work, but it's all going a bit over my head.
Thanks for taking the time to help me out, and I apologize for my inexperience.
Sure, it would be quite difficult. Why not try a simpler example game first? Do something on a flat surface.
Your answer
Follow this Question
Related Questions
Bike wobbles like pendulum after applying gravity 1 Answer
Js: UnityEngine.Rigidbody is required, how to fix? 2 Answers
How to set gravity for model? 1 Answer
Why can't I move along the Y axis when I shift gravity from the Y to Z axis? 1 Answer
Character teleports to the apex of full jump height 0 Answers