- Home /
Moving a point along a plane, mesh, or collider
If anyone has experience in this matter, your input would be greatly appreciated. I'm investigating different ways of implementing a system for a game, and I need some information on this option.
In Unity, is there an easy way to a fix a point, such as an Empty GameObject, to a surface such as a plane, a mesh, or a collision shape, and move it along that given surface.
If I remember correctly, the terrain system does something like this when its interacting with a character collider. I may be wrong, but I really just don't know where to start investigating this matter.
Answer by Wolfram · Aug 12, 2010 at 10:13 AM
Generally, you'd implement some form of terrain following. Using the position of your empty GO as origin, cast a ray straight down to find the intersection with (and therefore the distance to) your terrain. If you don't hit the terrain, you are below it, so cast another ray straight up.
Then you can modify the position/speed of the empty GO as you like. For example, just clamp it to the hit.point.
The other approach would be to let the physics engine do all the work: make sure you have colliders (and probably rigidbodies) attached to all your relevant objects, and use forces to move your object around. If you end up above your terrain, gravity will make you fall back to the surface. If your force is directed downwards at an angle, the physics engine will prevent it to penetrate the terrain.
Note that if you change the position of your GO directly, without the use of forces, you'd need to combine both approaches.
Can you give more details on what you're trying to accomplish? Like...do you want the object to actually be able to 'walk' or stick to the surface? If your point is on a sphere, it will need to continually 'stick' to the surface of the sphere as it 'walks' around it?
It almost sounds like you need to be able to get the normal of the face you are on, and constrain movement along that face plane. When you move to another face, your movement would be constrained to that face/plane. How you would get the "face" you are moving to, though...can Unity even provide that?