x


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:

alt text

untitled-1.jpg (55.7 kB)
more ▼

asked Aug 13, 2012 at 09:13 AM

GameMuncherz gravatar image

GameMuncherz
7 8 8 9

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

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.

more ▼

answered Aug 13, 2012 at 12:55 PM

mikebelotti gravatar image

mikebelotti
488 7 8 13

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.

Aug 13, 2012 at 01:56 PM GameMuncherz

Yes, it'll work for a cube. Just make sure to use -hit.normal as your gravity. If you were to instead 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/gamesdemystifiedsupermario.php

Aug 13, 2012 at 02:43 PM mikebelotti

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...

Aug 13, 2012 at 03:03 PM fafase

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. My character is in 3rd person and can go anywhere on the cube.

Aug 14, 2012 at 12:55 AM GameMuncherz

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 layerMask parameter) and the ray will only hit the cube. See the example here: http://docs.unity3d.com/Documentation/Components/Layers.html

Aug 14, 2012 at 12:46 PM mikebelotti
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 13, 2012 at 09:18 AM

Tim Michels gravatar image

Tim Michels
2k 34 29 42

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 }

Aug 13, 2012 at 12:44 PM GameMuncherz
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 13, 2012 at 09:26 AM

sam32x gravatar image

sam32x
183 159 128 129

(comments are locked)
10|3000 characters needed characters left
more ▼

answered Aug 13, 2012 at 03:14 PM

Griffo gravatar image

Griffo
2.7k 226 1228 320

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3127
x2800
x787

asked: Aug 13, 2012 at 09:13 AM

Seen: 1415 times

Last Updated: Jul 01, 2013 at 09:45 PM