Camera follow player (run forrest, run !!!)

Sorry by the title :laughing: :blush: :laughing:
So, I have some little problem here, I believe is easy to fix.
The code not is 100% working, so please focus on the process of camera.

I want put the camera back the player model, but how you’ll see, the player rotate in own axis ( getH() ) and when the player rotate, I want rotate cam too…

If I delete the comand cam.lookAt(player), I have a odd result because the camera rotate in a unknow axis…

def mover(self, task):
        self.isMoving = False
        #reseta todos os states por seguranca...
        for e in self.states :
            self.states[e] = 0

            
        newH = self.model.getH()
        newX = self.model.getX()
        newY = self.model.getY()
        
        if (self.directions["down"] != 0) and (self.directions["right"] != 0):
            newH -= 5
            #newX += (self.velocity + 0.01)
            newY -= (self.velocity + 0.01)
            self.isMoving = True
        elif (self.directions["down"] != 0) and (self.directions["left"] != 0):
            newH += 5
            #newX += (self.velocity + 0.01)
            newY -= (self.velocity + 0.01)
            self.isMoving = True
        elif (self.directions["up"] != 0) and (self.directions["left"] != 0):
            newH += 5
            #newX += (self.velocity + 0.01)
            newY += (self.velocity + 0.01)
            self.isMoving = True
        elif (self.directions["up"] != 0) and (self.directions["right"] != 0):
            newH -= 5
            #newX += (self.velocity + 0.01)
            newY += (self.velocity + 0.01)
            self.isMoving = True
        elif (self.directions["left"] != 0):
            newH += 5
            self.isMoving = False
        elif (self.directions["right"] != 0):
            newH -= 5
            self.isMoving = False
        elif (self.directions["down"] != 0):
            newY += (self.velocity + 0.01)
            self.isMoving = True
            
        elif ((self.directions["left"]!=0) and (self.directions["up"]!=0)):
            self.isMoving = True
            
        self.model.setH(newH)
        self.model.setX(newX)
        self.model.setY(newY)

        if (self.isMoving is True):
            if (self.statecurrent != "walking"):
                self.setEstado("walking", 1)
                self.model.stop()
                self.model.loop("walking")


        else:
            if (self.statecurrent != "static"):
                self.setEstado("static", 1)
                self.model.loop("static")

        if (self.camerastyle == "panoramic"):
            base.camera.setPos(self.model.getX()-30,self.model.getY()+220,self.model.getZ()+250)
            base.camera.lookAt(self.model)
        elif (self.camerastyle == "follow"):
            base.camera.setH(self.model.getH())
            base.camera.setPos(self.model.getX()-10,self.model.getY()+30,self.model.getZ()+5)
            base.camera.lookAt(self.model)
            
        return Task.cont       

Here two samples:

  1. Ok, follow the player in his back


By mcunha98 at 2009-08-04

  1. But, if I rotate, the cam stay in the original position and dont follow the angle of player…


By mcunha98 at 2009-08-04

use:

base.camera.reparentTo(self.model)

When you do that, all camera positions are now calculated as relative to the model, rather than relative to render.

So you can, for example,

base.camera.setPos(0,-40,10)

(Which is what I use) and get the camera hovering above and behind your model. As the model moves and rotates, the camera will go with!

Or for a little more smoothness/spring, parent a dummy node to the model, and have a background task that sets the camera’s pos/hpr incrementally towards the pos/hpr of the dummy (use, e.g., dummy.getHpr(camera.getParent())).

Tutunkommon

Very usefull your tip.
Works fine now.

But I have only a question, if I’ll need remove the parent of camera, I need reparent camera to what ? None or Render ?


By mcunha98

Back to ‘render’ again. ‘None’ is not a valid NodePath (it’s passed by value)

Ok, thanks !

use

wrtReparentTo(render)

so the camera doesnt jump when you do it.