Animation plays on key up?

Hi Guys,

What’s suppose to happen
I have a spaceship and when the ‘W’ key is press the ship is suppose to pitch up.

The ship then is suppose to look like it is pointing upwards and then moves,

What is happening
when I press the ‘W’ key down the Actor model moves and when I let go of the ‘W’ key the spaceship animation of the ship pointing up plays.

In Short…
Animation plays after key is pressed how do you fix this so that when key ‘W’ is down, play animation “up”

Thanks for all the help.

	def pitch(self, dir, dt):
		turnRate = self.handling * (2 - 
			(self.speed / self.maxSpeed))
		if(dir == "up"):
			self.playerModel.play("up")
			self.playerModel.setP(self.playerModel, turnRate * dt)

		elif(dir == "down"):
			turnRate = -turnRate
			self.playerModel.play("down")
			self.playerModel.setP(self.playerModel, turnRate * dt)
		self.playerModel = Actor("models/ship.egg",{
								"left" : "models/ship-left.egg",
								"right" : "models/ship-right.egg",
								"returnLeft" : "models/ship-returnLeft.egg",
								"returnRight" : "models/ship-returnRight.egg",
								"up" : "models/ship-up.egg",
								"down" : "models/ship-down.egg",
								"returnUp" : "models/ship-returnUp.egg",
								"returnDown" : "models/ship-returnDown.egg",
								"rollR" : "models/ship-rollR.egg",
								"rollL" : "models/ship-rollL.egg",
								"returnRollR" : "models/ship-rollLR.egg",
								"returnRollL" : "models/ship-rollLL.egg",
								})

Could we see the code that calls your “pitch” method, and any events that you’re accepting for “w” and “w-up”, please?

This is my player method where I use another class called input manager to handle key accepts.

player.py

def playerControl(self, task):
		dt = globalClock.getDt()
		if( dt > .20):
			return task.cont
		if(self.inputManager.keyMap["escape"] == True):
			return sys.exit #quit game
		if(self.inputManager.keyMap["w"] == True):
			self.pitch("up", dt)
		elif(self.inputManager.keyMap["s"] == True):
			self.pitch("down", dt)
		elif(self.inputManager.keyMap["w"] == False):
			self.camera.setP(0)
		elif(self.inputManager.keyMap["s"] == False):
			self.camera.setP(0)
		if(self.inputManager.keyMap["arrow_down"] == True):
			self.adjustThrottle("down", dt)
		elif(self.inputManager.keyMap["arrow_up"] == True):
			self.adjustThrottle("up", dt)
		if(self.inputManager.keyMap["0"] == True):
			self.throttleStop("stop", dt)
		if(self.inputManager.keyMap["arrow_right"] == True):
			self.turn("r", dt)
		elif(self.inputManager.keyMap["arrow_left"] == True):
			self.turn("l", dt)
		if(self.inputManager.keyMap["arrow_right"] == False):
			self.camera.setR(0)
		elif(self.inputManager.keyMap["arrow_left"] == False):
			self.camera.setR(0)
		if(self.inputManager.keyMap["a"] == True):
			self.yaw("r", dt)
		elif(self.inputManager.keyMap["d"] == True):
			self.yaw("l", dt)
		elif(self.inputManager.keyMap["a"] == False):
			self.camera.setH(0)
		elif(self.inputManager.keyMap["d"] == False):
			self.camera.setH(0)
		if(self.inputManager.keyMap["wheel-in"] == True):
			self.cameraZoom("in", dt)
		elif(self.inputManager.keyMap["wheel-out"] == True):
			self.cameraZoom("out", dt)
		if(self.inputManager.keyMap["i"] == True):
			self.viewImage("in", dt)
		elif(self.inputManager.keyMap["o"] == True):
			self.viewImage("out", dt)
		if(self.inputManager.keyMap["lalt"] == True):
			if(base.mouseWatcherNode.hasMouse() == True):
				mpos = base.mouseWatcherNode.getMouse()
				self.camera.setP(mpos.getY() * 180)
				self.camera.setH(mpos.getX() * -180)
		
		self.speedCheck(dt)
		self.move(dt)
		return task.cont

inputManager.py

from direct.showbase.DirectObject import DirectObject

class InputManager(DirectObject):
	def __init__(self):
		self.keyMap = {"w" : False,
						"s" : False,
						"a" : False,
						"d" : False,
						"wheel-in" : False,
						"wheel-out" : False,
						"+" : False,
						"-" : False,
						"0" : False,
						"i" : False,
						"o" : False,
						"arrow_left" : False,
						"arrow_right" : False,
						"arrow_up" : False,
						"arrow_down" : False,
						"escape" : False,
						"lalt" : False}
		
		self.accept("w", self.setKey, ["w", True])
		self.accept("s", self.setKey, ["s", True])
		self.accept("a", self.setKey, ["a", True])
		self.accept("d", self.setKey, ["d", True])
		self.accept("wheel_up", self.setKey, ["wheel-in", True])
		self.accept("wheel_down", self.setKey, ["wheel-out", True])
		self.accept("+", self.setKey, ["+", True])
		self.accept("-", self.setKey, ["-", True])
		self.accept("0", self.setKey, ["0", True])
		self.accept("i", self.setKey, ["i", True])
		self.accept("o", self.setKey, ["o", True])
		self.accept("arrow_left", self.setKey, ["arrow_left", True])
		self.accept("arrow_right", self.setKey, ["arrow_right", True])
		self.accept("arrow_up", self.setKey, ["arrow_up", True])
		self.accept("arrow_down", self.setKey, ["arrow_down", True])
		self.accept("escape", self.setKey, ["escape", True])
		self.accept("lalt", self.setKey, ["lalt", True])
		
		self.accept("w-up", self.setKey, ["w", False])
		self.accept("s-up", self.setKey, ["s", False])
		self.accept("a-up", self.setKey, ["a", False])
		self.accept("d-up", self.setKey, ["d", False])
		self.accept("+-up", self.setKey, ["+", False])
		self.accept("--up", self.setKey, ["-", False])
		self.accept("0-up", self.setKey, ["0", False])
		self.accept("i-up", self.setKey, ["i", False])
		self.accept("o-up", self.setKey, ["o", False])
		self.accept("arrow_left-up", self.setKey, ["arrow_left", False])
		self.accept("arrow_right-up", self.setKey, ["arrow_right", False])		
		self.accept("arrow_up-up", self.setKey, ["arrow_up", False])
		self.accept("arrow_down-up", self.setKey, ["arrow_down", False])
		self.accept("escape-up", self.setKey, ["escape", False])
		self.accept("lalt-up", self.setKey, ["lalt", False])

	def setKey(self, key, value):
		self.keyMap[key] = value

Pitch Method

	def pitch(self, dir, dt):
		turnRate = self.handling * (2 - 
			(self.speed / self.maxSpeed))
		if(dir == "up"):
			self.playerModel.play("up")
			self.playerModel.setP(self.playerModel, turnRate * dt)

		elif(dir == "down"):
			turnRate = -turnRate
			self.playerModel.play("down")
			self.playerModel.setP(self.playerModel, turnRate * dt)

Thanks for looking at this for me.

     if(self.inputManager.keyMap["w"] == True): 
         self.pitch("up", dt) 
     elif(self.inputManager.keyMap["w"] == False): 
         self.camera.setP(0) 

why do the up and down events of the same key have effects on different objects? one on a 3D model, the other on the camera ?

and does the setP function in playermodel mean setPitch?
if you want a 3D model the pitch up, why use instant value turnratedt, instead of accumulating the angle by adding turnratedt to previous angle ?

This was something I was playing with but have taken it out the ‘w’ key calls the pitch method passing the direction.

yes instead of setHpr(0,0,0) I am just using setP.

This was something I pulled out of a book and was using it to control how fast I want my model to turn. alot of the code you see there is going to be cleaned up.

My main concern and question is:
why does an animation play on a key-up and not on a key down?

do your animation file and Panda setting use the same coordinate system, such as Z-up or Y-up ?
or the “up” “down” animation are inverted ?

another concern , are you sure the
if …
elif …
elif …
elif …
used to test the w and s key status is the right way?

i am confused at that.
if w and s keys are controlling the same axis of rotation, try
dir=keymap[‘w’]-keymap[‘s’]
that produces -1 or 0 or 1

Ah, wait, I think that I see the problem: as your code currently works, “setPitch” is called repeatedly while the “w” key is held down. Within “setPitch”, you simply call “play”, which may mean that the animation is being restarted repeatedly as long as the key is held down, effectively locking it to the start of the animation until you release the key and let it play out.

In essence, I think that it’s not that your animation isn’t playing when the key is held, I think that it’s rather that your animation is being started over and over again until the key is finally released.

You might get it to work by only playing the animation if it isn’t already playing – this page should have more information on how to do that, I think.

(You may also find that you want to handle what happens when the animation finishes: loop the last few frames, or call “pose” to hold it on the last frame, for example.)

ahh Thaumaturge why didn’t i see that haha. thank you guys for taking the time to give me a hand.

ill go and change my code and will post back here if it’s fixed or not.

:slight_smile:

Hi guys

The advice you gave me was spot on I was able to see what was going wrong for me and am slowly fixing it. Thanks for the help

Rob