Trying To Figure Out How To Make Levitating Movement

Hi, I’m trying to make my camera kind of just float/hover in the air smoothly, in a way that it moves up and down, kind of like if its a drone on the air.

Of course this script wont do the job, but it does move it up and down, but I cannot figure out how I can slew this motion, and also how to have some sort of ‘flag’ that is at True or False if its reached its maximum height or minimum, to prevent it from going down again once its not in the minimum Z value.

What happens with this script currently is just this very glitchy bouncing, very rapid.
Anyone got tips on how to make this work? I got no idea. :stuck_out_tongue:

    def camHover(self, task):
        deltaTime = globalClock.getDt()
        camZ =
        cam =
        if camZ <= 20:
            cam.setZ(camZ + 1)
        if camZ >= 20:
            cam.setZ(camZ - 1)
        return Task.cont

Try this way, to achieve a smoother movement, you need to use the momentum formula.

from direct.showbase.ShowBase import ShowBase
from direct.task import Task

class MyApp(ShowBase):

    def __init__(self):


        self.scene = self.loader.loadModel("models/environment")
        self.scene.setScale(0.25, 0.25, 0.25)
        self.scene.setPos(-8, 42, 0)
        self.taskMgr.add(self.spinCameraTask, "SpinCameraTask")
        self.z = 10
        self.toggle = 0

    def spinCameraTask(self, task):
        if self.toggle == 0:
            self.z = self.z + 0.007
        if self.toggle == 1:
            self.z = self.z - 0.007

        if self.z >= 10.4:
            self.toggle = 1
        if self.z <= 10:
            self.toggle = 0
, 0, self.z)
        return Task.cont

app = MyApp()

This is a triangular movement.

1 Like

If the camera remains at a set altitude, then a simple approach might be to use a sine-function to smoothly move it up and down. Something like this:

time = globalClock.getRealTime()

cameraBob = math.sin(time * math.pi * 2 * self.cameraBobSpeed) * self.cameraBobRange + self.cameraBobAltitude)
1 Like