- Home /
Attraction by object shape (gravity)
Hello, is there a chance that the player was attracted by the shape (mesh)?
for example: when you reach the edge of the cube you can go off the wall without falling and then go to the bottom of the cube (upside down).
If so, how do I do that?
Answer by aldonaletto · Sep 22, 2012 at 09:20 PM
You will need a rigidbody character. I wrote a script times ago that allowed a rigidbody to walk upright over any surface - a local gravity pushed it towards the mesh under its feet, including in vertical or totally upside down surfaces. Take a look at my answer in this question.
Answer by Muuskii · Sep 22, 2012 at 06:07 PM
I just wrote this up without testing so take it with a grain of salt:
Ray ray = new Ray(Vector3.zero, Vector3.forward);
RaycastHit hitInfo = new RaycastHit();
if(Physics.Raycast(ray, out hitInfo) )
{
Vector3 hitNormal = hitInfo.normal;
}
What you could do is do a raycast towards the center of the cube and find the normal of the raycast hit like I show above. And then gravity = -hitNormal;
The gravity will always take your character towards the face of the cube he's standing on and if he falls off the edge it will take him above another face to stand on.
This "should" work for other shapes too.
Is this for a "minecraft planet"? I have to ask because I've wanted to see one. Without the motivation to actually do it :P
Small note: Putting the origin of your character's raycast at his feet will probably make this much better for what you're making.
Also; If you make the raycast only activate when the character is inside a trigger you can make sure to only do these tests when near an edge.
Thank you very much! Yes, there is a concept of the '$$anonymous$$ecraft' world composed of cubes, after which the player can move through walls and ceilings. Game will be based on FPS shooter game :)
I'll check of the script tomorrow, because today, unfortunately, I have no way to do this :(
Your answer
Follow this Question
Related Questions
Character attract from magnet object 0 Answers
Changing gravity affecting 'random movement' behaviour script - my NPCs go crazy, what should I do? 1 Answer
Gravity.cs help? 3 Answers
Walking on walls JS 2 Answers
Artificial Gravity 2 Answers