HI,
I have a cube representing the head of a snake. What i want is a way in which i can make the snake translate on the surface of a cube. Foe example if the head is on the XZ plane and when it goes out of the bounds of the plane it should rotate downwards to the other face of the cube and use that face as the ground.
1. How do i determine if the head has gone out of a particular face bound. 2.How do i align the head with the new face and keep it attached to the new face.
one way to do it could be to use six planes fitted together as the cube instead of a single cube.
You could then more easily determine which plane the snake is in contact with.
You could also move the snake relative to that plane using InverseTransformPoint or setting the snake as child of the active plane and using localPosition and/or localRotation.
If the size of your cube or the size of the planes are predetermined then it should be pretty easy to know when you have arrived at the boundary edges.
You can Raycast from the head to the center of the cube and find the surface normal. Then simply align the head to this normal. Navigating a cube or sphere is no different than navigating regular terrain. Given the cube’s sharp, 90 degree edges, it might be harder to get the head to transition smoothly. That will depend on exactly the type of motion you’re looking for.
Why’s that? You can align an object to the normals of a mesh regardless of it’s Y position. You’ll want to Raycast every frame, and Raycast to and from the proper positions, in the proper directions. If you were at the bottom of the cube so to speak, you’d Raycast up, towards its center and flipping won’t be an issue.
You will always get the normal to align to, but keeping the object aligned to that normal continuously seems to be the flipping problem. I’ve tried some approaches and got flipping either at the poles or at the equator when trying to navigate a sphere and remain upright and retaining a forward direction, so when considering a cube it just seemed more direct to work with the local space of each face/plane.
Odd. Is that behavior specific to character controllers? Or you mean the normals are messed up at the poles of a sphere? Cubes may not have this problem? I haven’t tried, so I can’t be certain, but it seems like it should work.
How can i make an object stick to the face of the cube if i am translating and go out of bound for a face. Object should snap to the next face of the cube below like fall and latch??
sorry to hijack, but do you know if it is the x, y or z term of the normal which causes the flipping? Maybe we could just multiply by -1 when the controller is in the bottom half of the sphere to stop the flipping?
@liquidgraph - I suggested of the use of six planes and using local/parent space not only as a means to move and steer the snake, but also when reaching the edges a coroutine could briefly take over and rotate the snake based on the plane’s local space to the next plane- as you noted, that would be the tricky part for smooth transitions. I introduced it only as one possible solution. A method with raycasting may also work. You are right the snake may not flip as it travels below -Y on the side of the cube, but I think the flipping problem would occur when transitioning onto the bottom side, as the calculation of the forward axis can flip as the normal transitions from a +y direction to -y.
@TheRaider - I have spent some time thinking about your thread because I really liked the mechanic in Mario Galaxy for the Wii, being able to run around little planets. I am still thinking about a method for doing it that could be more of a hierarchy based rig that would be dependent on the concept of running around spheres, but you may be able to solve it by the method you are considering.
@rakesh - I hope you get the snake game working! You may be able to use downward raycasting from the snake as a solution, or you could do it based on known dimensions of the cube and do rotational transitions when crossing from one face to the next. If the dimension of the cube is known, you then know your snake needs to ‘stick’ to a specific value depending on which side of the cube it is on. (e.g. if the cube is 10 units in size, and the snake is on the face that is facing on +x, then the snake needs to move around on the YZ plane always fixed at 10 units on x, and cannot go any further than -10 to 10 units on Y or Z without transitioning to the adjacent face)
I actually kind of got it working by doing all the rotations/calculating pependicular manually. However cause I keep track of everything I can’t jump to another planet.
I think the way I suggest would work on flipping the normal since that is what is causing the problem, however the vector math is beyond me
Hi,
I made it work finally. thanks for all your suggestions. Basically what i did was kept the count of the bounds on each plane face. i.e the min x min y max x max y and check if the snake is in the bounds and if it goes out side then ray cast from the center of the snake to the center of the cube… and take the normals and align the snakes rotation accordingly…