- Home /
false gravity on a cube planet
How do you change your characters gravity and rotation so that whatever face of the cube your on, your feet point to it and the gravity pulls you toward the face. I have somewhat of an idea but I'm having trouble with the code. heres my idea:
Answer by mikebelotti · Aug 13, 2012 at 12:55 PM
You only need one raycast. Cast from your character's position to the center of the cube. hit.normal
will be the surface normal of the side of the cube your character is closest to (and, therefore, which side is pulling him). Have your character's up vector become hit.normal
, and then pull your character in the direction of -hit.normal
. Doing it this way will work for more complex convex objects (like a capsule or cylinder) and ones that rotate too.
Will this work for a cube too though because i remember seeing something like this for a sphere. Wouldnt my character start acting all funny when it gets farther away from the center of the cube.
Yes, it'll work for a cube. Just make sure to use -hit.normal
as your gravity. If you were to ins$$anonymous$$d use, say, the vector from your character to the center of the cube, then you would get funny behavior because the gravity would appear be stronger when your character is on one of the corners of the cube (because, in that case, he is in fact further from the center). So yeah; don't do that.
This might be a useful read: http://www.gamasutra.com/view/feature/3593/games_demystified_super_mario_.php
True, only one ray is useful. For my answer, I used 4 rays as I thought of a way to change gravity in a whole world to stick on walls and ceilings...
Idk how to use on normals. Do I use Physics.Raycast to hit the center, like from my position to cube position? and for the gravity do I use AddForce or velocity? Sorry with all these question but this problem is really making my head spin. I've read that article before and I've tried the gravity on a sphere, but cubes seemed to work better in my game. Oh and if i enter a building or stand on a different object, will this mess anything up?
@fafase I liked your idea but my cube is using all 6 sides. $$anonymous$$y character is in 3rd person and can go anywhere on the cube.
Yes, you cast from the character's position to the cube's center. And if you put other stuff on the cube's surface, like buildings, just make sure they are on a different layer than the cube. Then, you can pass the cube's layer into Physics.Raycast
(the layer$$anonymous$$ask
parameter) and the ray will only hit the cube. See the example here: http://docs.unity3d.com/Documentation/Components/Layers.html
Answer by Tim-Michels · Aug 13, 2012 at 09:18 AM
You could add several triggers which act as forcefield. That way you should disable gravity itsself, but in the OnTriggerStay of the triggers, you could add force in certain directions.
edit
You can also use the OnTriggerEnter and OnTriggerExit to manage the rotation of you character.
I was thinking of doing that but i have to change the collider size everytime i exit or enter a collider beacuase if i dont, the edges of the cube will have empty space or intersected colliders. i have 6 colliders by the way. So like function OnTriggerEnter(){ find collider with this tag. change the size change my characters gravity }
Answer by sam32x · Aug 13, 2012 at 09:26 AM
here's a probably bad idea but it might be good use transform.LookAt to point the character at the cube and check to see if the x, y and z rotations are divisible by 90, if they aren't then they go back to what they were last frame but if they are then they stay like that.
Answer by Griffo · Aug 13, 2012 at 03:14 PM
Have you seen this?
http://forum.unity3d.com/threads/33987-Character-align-to-surface-normal
Your answer
Follow this Question
Related Questions
[C#] Raycast based physics and clipping 0 Answers
Applying Normal force from ground to player 1 Answer
Raycasting problem 2 Answers
Calculate where an object is going to land 0 Answers
Edit this script so that on the second jump change the gravity to 5 2 Answers