Delayed Parenting

How would I make it so that if I parented an object to another, (In the Hierarchy) like a weapon to the camera, that it would have a slight delay before it reached the camera? For example, In Portal and Portal 2, when you move the camera fast, the portal gun has a little delay before it catches up with the camera, as if your hands couldn’t move as fast as your eyes.

Your going to have to keep track of some new state information for the camera. You’ll need to know when its stationary. So if there hasn’t been any movement on x,y axis then stationary = true. The algorithm you want to follow is something like this:

As soon as you move if stationary is true you set it to false. Also during this one frame save the current position and set some timer = 0. While your moving timer+=time.DeltaTime ,and set your gun to the currentPosition. Your camera keeps moving but you keep setting the gun behind you back at the saved currentPosition. Then after X timeDelay (when timer >= weaponLagTime) or stationary= true. Lerp the gun from currentPostion to the actual cameraPosition.

Just Lerping the object towards its target position each frame should be enough.

1 Like

That is true. I wasn’t thinking about variable speeds. This actually sounds like a job for SmoothDamp. It just takes a reference to the endPosition and a velocity. So everytime the camera moves you just make sure some bool on your gun is set to true and during Update of the gun Use SmoothDamp to move towards the camera position. Just set your velocity to be slower than the camera can move.

Thank you (: