Has anyone ever done such a thing? I’m envisioning an object (the rigidbody) which tries to follow a path, but can also be effected by collisions with its environment. If it collides with something and gets pushed off course, it tries to re-acquire its path.
Anyone ever do such a thing or have any idea how it might be done in Unity?
My first thought is that you could have an invisible animated object with a joint (maybe ConfigurableJoint for maximum, uh, configuration) attached to it and the rigid body. The rigid body would then follow the animated object until it runs across something that creates a force or torque past the breaking point defined in the joint. Or for the re-acquiring path option just try it with a SpringJoint.
Hmmm… Interesting idea, I haven’t really used ConfigurableJoints before but you’ve given me something to research.
I gave this a bit more thought since my first post and what I’m thinking, though, is that the object should probably have some sort of “avoidance radar” so that it steers clear of obstacles in its path, otherwise if something is directly in front of it, it might just “stick” there.
So I’m thinking that the object would have to use relative force and relative torque to try to follow the path (or an invisible object that’s following the path) but have the ability to veer off course to avoid an obstacle in its path. And also be able to reacquire the path if a collision pushes it off… starting to sound pretty complicated huh? I wonder where the best place to start might be?
You don’t need path finding if you just want a preset path. For that you just do path following which is simply a list of nodes (positions in space, or custom classes, whatever), and you seek towards the current node until you get within a certain range of it, then you switch to the next node in the list and so on.
The spring joint ideas sound like a good way so keep it dynamic. Just use the path following code on the control object.
You need a collection of markers which are listed in sequence. A handy technique is to use a 3D modelling app to make a sequence of disconnected vertices (ie, no triangles defined) - most modellers keep the verts listed in the order you define them. You can get the verts within Unity through the Mesh class. If you don’t like this approach, you can use empty objects as markers and use a naming convention to imply a sequence.
You then simply use forces to move your rigidbody to the target marker. When you get close to the target, you change it so that the next marker in the sequence becomes the target and so on. If your object gets knocked so far off course that it is a long way from the target, you can have a backup “find nearest marker” mode until you get back on track.
You could certainly do something like this. If you want something a bit more straightforward, you could use another force to push the moving object around the obstacle (try using rigidbody.AddExplosionForce with the force emanating from the obstacle’s centre).
It really depends on what the moving object is. Using a simple sidestep won’t look very natural for a human/animal figure, where you’d do better with a bit of AI. At the other extreme, something like a camera will often work very well with simple physical motion and AI would just add unnecessary complexity.
The object I’m thinking about would be something like an underwater torpedo. We don’t have to worry about gravity with this object, so any force on the object should veer it in that direction. But a torpedo relies on forward thrust in order to turn, so our collision avoidance probably needs to check for objects fairly far ahead of the object and then make very small torque forces to veer it from the object.
You could give your torpedo some “whiskers” - pairs of rays projecting either side of the object so they diverge to roughly the width of the torpedo at the distance you want to start reacting to obstacles. With the rays arranged in left-right pairs, you can easily detect which way to dodge. A nice heuristic is to make the torque on the torpedo increase the shorter the raycast hit distance, so it will turn sharply if an obstacle suddenly appears up close.
if you’re going to have lots of torpedos active maybe you could also put a large invisible trigger collider on your obstacles (10M or whatever distance) that turn on the raycasts only when in range for better performance.