Too fast monsters

In the development of:

I am making monsters that attack the player. But they are coming too fast and don’t give the time for the player to attack (The monster doesn’t damage but will in the future).

Code and models and stuff (Spoiler)
from direct.showbase.ShowBase import ShowBase
from direct.actor.Actor import Actor
from direct.task import Task
from panda3d.core import *
loadPrcFile("config/config.prc")

class Game(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        
        self.disableMouse()
        
        self.cTrav = CollisionTraverser()
        self.pusher = CollisionHandlerPusher()
        self.pusher.setHorizontal(True)
        
        self.room = self.loader.loadModel("models/room.egg")
        self.room.setH(270)
        self.room.reparentTo(self.render)
        
        self.camera.setPos(0, -20, 2)
        
        self.keyMap = {"w" : False, "a" : False, "s" : False, "d" : False, "arrow_left" : False, "arrow_right" : False}
        
        for key in self.keyMap:
            self.accept(key, self.updateKeyMap, [key, True])
            self.accept(key + "-up", self.updateKeyMap, [key, False])
        
        self.taskMgr.add(self.update, "update")
        
        camCenter = self.camera.getBounds().getCenter()
        camRad = 2
        cNode = CollisionNode("camera")
        cNode.addSolid(CollisionSphere(camCenter, camRad))
        cameraC = self.camera.attachNewNode(cNode)        
        
        self.cTrav.addCollider(cameraC, self.pusher)
        self.pusher.addCollider(cameraC, self.camera)
        
        wallNode = CollisionNode("wall")
        wallSolid = CollisionCapsule(-4, -6, 1, -1, -6, 1, 0.5)
        wallNode.addSolid(wallSolid)
        wallC = render.attachNewNode(wallNode)
        
        wallNode = CollisionNode("wall")
        wallSolid = CollisionPlane(Plane(Vec3(1, 0, 0), Point3(-5, 0, 2)))
        wallNode.addSolid(wallSolid)
        wallC = render.attachNewNode(wallNode)
        
        wallNode = CollisionNode("wall")
        wallSolid = CollisionPlane(Plane(Vec3(0, 1, 0), Point3(6, -15, 2)))
        wallNode.addSolid(wallSolid)
        wallC = render.attachNewNode(wallNode)
        
        wallNode = CollisionNode("wall")
        wallSolid = CollisionPlane(Plane(Vec3(-1, 0, 0), Point3(5, -5, 2)))
        wallNode.addSolid(wallSolid)
        wallC = render.attachNewNode(wallNode)
        
        wallNode = CollisionNode("wall")
        wallSolid = CollisionPlane(Plane(Vec3(0, -1, 0), Point3(-3, 5, 2)))
        wallNode.addSolid(wallSolid)
        wallC = render.attachNewNode(wallNode)
        
        self.monster = Actor("models/monster1.egg", {"attack" : "models/monster1-pincer-attack-both.egg", "explode" : "models/monster1-explode.egg"})
        self.monster.reparentTo(self.render)
            
    def updateKeyMap(self, key, value):
        self.keyMap[key] = value
        
    def update(self, task):
        dt = globalClock.getDt()
        
        if self.keyMap.get("w"):
            self.camera.setY(self.camera, dt * 10)
        elif self.keyMap.get("a"):
            self.camera.setX(self.camera, dt * -5)
        elif self.keyMap.get("s"):
            self.camera.setY(self.camera, dt * -10)
        elif self.keyMap.get("d"):
            self.camera.setX(self.camera, dt * 5)
        elif self.keyMap.get("arrow_left"):
            self.camera.setH(self.camera, dt * 40)
        elif self.keyMap.get("arrow_right"):
            self.camera.setH(self.camera, dt * -40)
            
        self.monster.lookAt(self.camera)
        self.monster.setY(self.monster, 1)
        
        return Task.cont
        
game = Game()
game.run()

config.zip (184 Bytes)
icon Convert to .ico
models.zip (921.5 KB)

If I’m reading this correctly, you’re moving your monsters forward, relative to themselves, at a rate of one unit per frame.

To start with, you should be able to slow down your monsters by simply changing the value “1” to something smaller.

However, there’s a problem: you’re moving your monsters by the same amount each frame, regardless of how much time has passed since the last frame. This means that, the faster your game runs–that is, the higher the frame-rate–the faster your monsters will move.

To deal with this, simply incorporate the delta-time value, as you already do for the camera.

So I should do:

self.monster.setY(self.monster, dt * 10)

EDIT: I also made a collision detection for the monster so it doesn’t glitch the screen. It is now also at the scale of 0.25.

That looks decent, indeed. :slight_smile:

That’s good to read. :slight_smile:

I do have two caveats, if I may:

  1. Since you’re moving your monster relative to itself, the monster’s scale will affect that movement, if I recall correctly. If this is fine, then well and good! At the least it’s worth being aware of. Similarly, it’s perhaps worth noting that it’s possible to have this not be so, although that calls for a slightly more complex approach.

  2. If that scale applies to the collision object–whether because you scaled the collision-object itself or because you scaled a parent-node of the collision-object–then you might possibly have problems: scaling doesn’t always mesh well with collision, I believe.

Everything works perfectly with what I changed it to do. Thanks anyways!

1 Like