My character is not moving

Hi Guys,

I’m new to panda3d and to python, so be gentle :wink:

I took some code from the roaming-ralph tutorial and other stuff and put all in my own script. Everything works fine, but my character is not moving when there is a move-key pressed. Also, the animation is looping and never stops.

import direct.directbase.DirectStart  #Initialize Panda and create a window
import random, sys, os, math, time, threading, socket          #libs
from direct.gui.OnscreenText import OnscreenText
from direct.gui.OnscreenImage import OnscreenImage
from direct.gui.DirectGui import *
from direct.task.Task import Task     #Task run
from import Actor
from pandac.PandaModules import Fog
from pandac.PandaModules import *
from pandac.PandaModules import WindowProperties
from pandac.PandaModules import Shader
from pandac.PandaModules import Point3,Vec4
from pandac.PandaModules import NodePath
from direct.filter.CommonFilters import CommonFilters
from pandac.PandaModules import CollisionTraverser,CollisionNode
from pandac.PandaModules import CollisionHandlerQueue,CollisionRay
from direct.showbase.DirectObject import DirectObject

class Avatar(DirectObject):
    def  __init__(self):

        environ = loader.loadModel("models/world/world")
        self.ralph = Actor("models/ralph/ralph",{"run":"models/ralph/ralph-run","walk":"models/ralph/ralph-walk"})
        self.keyMap = {"left":0, "right":0, "forward":0, "back":0,}
        # Accept the control keys for movement
        self.accept("escape", sys.exit)
        self.accept("w", self.setKey, ["forward",1])
        self.accept("a", self.setKey, ["left",1])        
        self.accept("s", self.setKey, ["back",1])
        self.accept("d", self.setKey, ["right",1])        
        self.accept("w-up", self.setKey, ["forward",0])
        self.accept("a-up", self.setKey, ["left",0])
        self.accept("s-up", self.setKey, ["back",0])
        self.accept("d-up", self.setKey, ["right",0])        
        #Dummy over the avatars head
        self.dummy = NodePath(PandaNode("floater"))
        # Enable a 'light ramp' - this discretizes the lighting,
        # which is half of what makes a model look like a cartoon.
        # Light ramps only work if shader generation is enabled,
        # so we call 'setShaderAuto'.
        tempnode = NodePath(PandaNode("temp node"))
        tempnode.setAttrib(LightRampAttrib.makeSingleThreshold(0.5, 0.4))
        # Use class 'CommonFilters' to enable a cartoon inking filter.
        # This can fail if the video card is not powerful enough, if so,
        # display an error and exit.
        self.separation = 1 # Pixels
        self.filters = CommonFilters(,
        filterok = self.filters.setCartoonInk(separation=self.separation)
        # Create a non-attenuating point light and an ambient light.
        plightnode = PointLight("point light")
        plight = render.attachNewNode(plightnode)
        alightnode = AmbientLight("ambient light")
        alight = render.attachNewNode(alightnode)
        #position of the dummy
        def dummyPos(task):
            pos = self.ralph.getPos()
            z = self.ralph.getZ() + 5
            return task.cont

        taskMgr.add(dummyPos, "dummyPos")
        #the camera is bound to the dummy over the avatars head
        def camPosition(task):
  , 25, 5)
            return task.cont
        taskMgr.add(camPosition, "camPosition")

    def setKey(self, key, value):
        self.keyMap[key] = value
        #movement of the avatar
        def move(task):
            elapsed = globalClock.getDt()
            if (self.keyMap["left"]!=0):
                self.ralph.setY(self.ralph, -1)
            if (self.keyMap["right"]!=0):
                self.ralph.setY(self.ralph, 1)
            if (self.keyMap["forward"]!=0):
                self.ralph.setY(self.ralph, 1) 
            if (self.keyMap["back"]!=0):
                self.ralph.setY(self.ralph, -1)
            return task.cont

        self.isMoving = False
        if (self.keyMap["forward"]!=0) or (self.keyMap["left"]!=0) or (self.keyMap["right"]!=0):
            if self.isMoving is False:
                self.isMoving = True
            if self.isMoving:
                self.isMoving = False

avatar = Avatar()

I know, ralph is “flying” in the air and there is no collision detection implemented. But this shouldn’t be the reason for this problem, right? It is just for testing and i want to go step by step and really understand what i am doing… I already tried to figure out what is wrong, by looking at other codes and the tutorials, but I have no clue…

If you could help me, i would be very happy!



You need to add move to the task manager.


edit: fix capitalization

Check your indenting as well. It looks like everything is defined inside init up to setKey, which is out-dented. What that does is defines move() inside of setKey now, which is probably not where you want it.

Oh, i feel like a dork! This error was so obvious and I did not see it, very embarassing :-/

Thanks for the fast help! And you are right, the indenting is not very clever, I am working on it. Still work in progress.