- Home /
Moving rigidbody along spline, and being able to use physics
Hi,
So I've been trying to make a rigidbody along a spline that I've created, but I'm stumped about how I'm supposed to do this while the rigidbody (right now, and for testing a box) should stay on the spline, while also keeping it in the physics-simulation. I've seen examples done in iTween, on this examples page, namely the "Path-constrained characters" one, and "Using PutOnPath and PointOnPath", but it seems like the objects accelerates when points have higher distances between eachother.
What I have done:
I can fetch points on the spline (made by a bunch of bezier curves)
I can fetch them by time-percentage, but also on distance, because I've done an arclength approximization, so I can get a constant speed when moving on the spline.
I think those are the vital parts for getting this to work(?). What I want to do is getting the object to move along the path, even if the speed to move along the path would be high, and I want the object to still reach to obstacles, hills and so on that I would put on the path. Anyone has good idea?
Thanks for reading (and hopefully helping =) )
Say wahhhhhh (In other words, im cinfused and u lost me lol)
I'm curious myself; I've been trying to find a similar solution and am experimenting with some code to make it happen. If I have a breakthrough, I'll let post it here.
You may want to elaborate what exactly you mean by physics-simulation. For instance when the rigidbody hits a collider along the spline, should it stop? Should it bounce back? What if the physics simulation sends the body off the spline, should it snap back to the spline? Should it transform its momentum along the direction of the spline?
If what you want is something along the lines of rigidbody constraints but on a variable axis (i.e. spline), I'd suggest simulating the velocity.
So for a given fixedupdate with rigidbody.velocity, the distance traveled will be rigidbody.velocity.magnitude * Time.fixedDeltaTime. Add that distance to the current position on the spline and figure out the resulting position, which is where you should move the rigidbody to with rigidbody.$$anonymous$$ovePosition
You'll need extra code to figure out the direction like when you actually want the body to go back along the spline in reaction to a collision.
Answer by hexagonius · Jan 16, 2016 at 03:35 PM
As taylank said, calculate either the next position the rigidbody should go and move it there with MovePosition, or calculate the velocity towards that point and set its velocity. Either way, do this in FixedUpdate(), that will keep all changes in the physics timestep and it will work physically. Notice that deltaTime IS fixedDeltaTime in the FixedUpdate loop by default, so no need to use fixedDeltaTime there.