I used to have a sticky grenade script that I’ve found some years ago but lost now. This was working with a dynamic parenting system using collision to parent an object (throwable) to another. If anyone could propose me a script like this one it would be very useful for everyone :eyebrowlift:
I don’t think you need a script to achieve this.
To your sticky grenade that you spawn in (through an empty etc.)
- Add a collision sensor (and leave it blank) however make sure you make the spawner no collision!
- Join the collision sensor to an edit object actuator
- Change add object to dynamics
- select suspend dynamics (if your grenade has dynamic movement or select disable rigid body for rigid body movement)
- Add a delay (maybe 120 or 2 seconds), and then spawn in your explosion (edit Object) and delete the grenade object (end Object)
Hope this helps, no parenting needed ;).
-Thatimst3r
This won’t “stick” to a moving object, which is what CarlJohnson is looking for. For other uses though, a good solution
def stick_to(cont):
own = cont.owner
collision_sensor = next(s for s in cont.sensors if isinstance(s, bge.types.KX_TouchSensor))
if not collision_sensor.positive:
return
hit_obj = collision_sensor.hitObject
own.parent = hit_obj
For use in module mode, with a collision sensor
I don’t think you can use own.parent=
I think you need own.setParent(sens.hitObject,0,1)
I might be doing something wrong in my setup, but your method isn’t working.
I’ve got a grenade running the script, with a default Collision sensor attached to it.
Whenever the grenade is spawned, launched with a linear velocity, and then makes contact with another object, I get this in the console:
line 11, in stick_to
AttributeError: attribute 'parent' of 'KX_GameObject' objects is not writable
The python API shows that the “hitObject” property is read-only; perhaps that is the cause?
replace
own.parent = hit_obj
with
own.setParent(sens.hitObject,0,1)
collision--------------python
import bge
cont=bge.logic.getCurrentController()
own=cont.owner
sens=cont.sensors['Collision']
if sens.positive:
own.setParent(sens.hitObject,0,1)
NOTE: this relies on the collision sensor being named Collision and connected to the controller
EDIT: Nevermind…
Thank you, BPR!
No problem, Happy Blending!
Thanks Everyone !