How to Parent a Sprite to Objects in Real Time?

How would i make a Sprite get Parented to a Game Object in Real Time? Similar to a Bullet Hole or Blood Splatter in a FPS Game?

My script so far:

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

collision = cont.sensors["collision"]

decal = cont.actuators['decal']

if collision.positive:
    cont.activate(decal)
    sprite = decal.objectLastCreated
    sprite. ?????

Older Version of the Script I Couldn’t get to work:

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

collision = cont.sensors["collision"]
decal = cont.actuators['decal']

if collision.positive:
    cont.activate(decal)
    sprite = decal.objectLastCreated
    sprite.alignAxisToVect(collision.hitObject)
    target = collision.hitObject
        sprite.setParent = target

ok so here i have another version of the script but how would i make it so the object aligns to the normal it hit ?

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

collision = cont.sensors["Collision"]
parent = cont.actuators["setParent"]

if collision.positive:
    target = collision.hitObject
    parent.object = target
    cont.activate(parent)

the only thing i see, that is wrong ( might have worked in the past…) is setParent = target
try the old code and replace that with setParent(target)
Because setParent() is a function, it needs to have the argument, in your case “target”.

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

collision = cont.sensors["collision"]
decal = cont.actuators['decal']

if collision.positive:
    cont.activate(decal)
    sprite = decal.objectLastCreated
    sprite.alignAxisToVect(collision.hitObject)
    target = collision.hitObject
    sprite.setParent(target)

does that work?

1 Like

so I was able to make it parent in real time but how would I make it align to the normal it collided with ?

you need get - vector to object -
vect = own.getVectTo(object_orient)
own.getAxisVect(-vect[1], 1, 1.0) # own its sprite, vect - position vector, getAxisVect orientation object another object or position
or you use billboard or halo surface orientation in material setup this rotation surface sprite in active camera in you game scene