Trampoline physics/UPBGE

I’m trying to set up a trampoline-like object in my game, and I can’t get a working code in place. My player object is a character, and I have this script connected to a collision sensor named “spring”. Here’s the code:

from bpy import data

cont = bge.logic.getCurrentController()

own = cont.owner

spring = cont.sensors['Collision']

if spring = positive:
    bpy.data.objects["Cube.001"].game.jump_speed = 200
    
else:
    bpy.data.objects["Cube.001"].game.jump_speed = 40

There’s probably something wrong with my code (I’ve only just started learning), but from where I stand it looks fine to me. Can anyone point me in the right direction?

this could work by flipping the projectile object’s z velocity upon collision.

example:

have a collision sensor hooked up to the python controller

collisiion === python

example code:

from bge import logic

col = logic.getCurrentController().sensors['Collision']
own = logic.getCurrentController().owner

if col.positive:
      vel = own.getLinearVelocity()
      vel.z *= -1
      own.setLinearVelocity(vel)

couple of things to improve this. first, own.localLinearVelocity and own.worldLinearVelocity are easier to read and are read/write attributes.

second, might want to run a if own.worldLinearVelocity.z < 0: check to be sure you arent inverting an object already inverted to prevent you from getting stuck there.

1 Like