- Home /
Add force along a path
Hi!
I can't seem to figure out a way to push an object along a path using AddForce. My goal is to only limit the players x axis along a spline, and keep the y and z axis free for jumping and going forward/backwards using AddForce.
I've seen some good examples out there like itween and spline walker, but these only reposition the object, and does not use physics to create movement.
Does anyone know how to achieve this effect?
EDIT: During my testing I've made use of a script that I found on this site: http://catlikecoding.com/unity/tutorials/curves-and-splines/ Maybe I can make this work if I can calculate the closest point to the player on the Bezier Spline. Does anyone know what piece of code I would need for this?
Thanks!
I'm making several assumptions about the nature of your game, but I don't think you'll get the behavior you're expecting. The best your approach can do will produce undesirable effects when your body interacts with others, and you will not follow the spline directly; it'll be more of a suggestion, like: Seek this X position by adding force on X whose magnitude is affected by current distance from desired X. This isn't technically spline-following, nor will it produce guaranteed-reasonable results, especially when other bodies are involved in the simulation.
Tasks like this aren't necessarily suited to the physics system. (You could still use the system to check for collisions, but drive motion without physics and use a kinematic rigidbody).
Don't let me talk you out of it, though. It's worth a try and might meet your needs depending on what you expect!
Anyone care to challenge my wisdom here? I always enjoy being proven silly or just plain wrong.
Hi Sunny!
Thanks for your feedback! Sorry if I'm not understanding you correctly, but I don't want to seek x by applying force. I mean that I want the x axis to always be locked to the spline, but affect y and z by force. $$anonymous$$ind of like the behavior you get from freezing rotation or position on a rigidbody; You can collide with other objects, but limited to the locked axis. I'll attach an image if that describes my vision clearer. :)
Ah, my apologies if I misread; I definitely misunderstood. Now I understand that you mean the body's local X relative to the spline is clamped. I dig your ALLCAPS script; I do that too, especially w/ a tablet.
This question just got interesting. No trivial-yet-ironclad approach springs to $$anonymous$$d, but I'm along for the ride.
I assume because you're wanting to use forces to drive some aspects of motion, you want this body to physically interact with some part of the world, right? What should happen when this body collides with another? What types of collision events do you expect?
What kind of data can you pull from the spline? Can you, for instance, sample two nearly-adjacent positions A & B to find the forward direction with (B-A).normalized?
As I think on this, I'm wondering if there actually is a "good" way to make this happen. The physics simulation is always in world space, and you're essentially wanting it to work in object space.
I guess you could find the object's local X direction each FixedUpdate and nullify all velocity in that direction, but I'm not sure how that would behave when the body interacts with another. At best I think it would force the other object aside in a physically unrealistic manner and compromise the purity of the simulation on the two unclamped axes.
"Or maybe we can get away with constantly altering the velocity in the direction of the spline at the closest point?"
I believe that's as good as you're going to do without breaking away from the physics simulation altogether. Consider though: Velocity changes always necessarily disrupt the purity of the physics simulation, meaning the forces you apply are not the forces you will observe in practice. If you are applying forces and changing velocities every frame, you are not, for all intents and purposes, using the physics simulation as intended. It can also cause issues when the body interacts with another.
Answer by nirharpaz · Feb 10, 2015 at 08:35 AM
I think I have an idea how I would approach that.
every frame the obj has a position, he calculate his position comparing to the spline and move another step. so if i look of each step.. there is the middle of the object and the middle of the spline. if the spline is straight the middles are merged and you move forward. in fact you always move forward. diff is when the middles that you are handling to next step are not merged - same direction and position. in this case I would calculate the difference in rotation and movement and add force and rotation towards potting the obj middle on the spline middle.
that's my rough idea