Object Moves When Game Starts

I’ve been working on a system to use mesh vertices to place a grid of blocks (Similar to a rain system I have done that shuffled objects around). I have a script that makes the child empty run through its parent’s vertices adding cubes if a near sensor is False. This works and keeps the framerate stable, but recently my mesh has been teleporting to [0,0,0] when the game starts. This causes the blocks to be put where I don’t want them and the near sensor on the empty is off (meaning it keeps making blocks). Before this happened I tried resetting the parent object’s position, but had to delete it and start again. All new meshes I make seem to have this problem. Is there a fix?

you can skip the first frame either by using states or combining the sensors with an inverted delay sensor.

Thanks, but I’m unsure that would work since it appears that either the object or the mesh is moving when the game starts. I used print(own.position) ant it looks like just the mesh is moving independent of the object center. Shouldn’t the mesh stay with the object center?

Post a file that we might be able to debug. From the initial observation, it doesn’t sound a familiar problem.

BrokenTestPlace.blend (461 KB)

This should show the problem.

A few things:

  • The near sensor won’t update until the next frame, after all blocks have been added.
  • The check “if x == False” is better done with “if not x”
  • You’re spawning on vertex coordinates, in object space. You need to convert them into world space.
  • Don’t use .position. it’s deprecated because it’s ambiguous.
  • Don’t use script mode, it’s pretty clunky :wink:

from bge import logic


# place blocks where the empty is.
def place_block(obj, vertex_position):
    x = round(vertex_position.x)
    y = round(vertex_position.y)
    z = round(vertex_position.z)        
    
    block = obj.scene.addObject("Cube", obj)
    block.worldPosition = [x, y, z]


def place(cont):
    own = cont.owner
    scene = own.scene
    near = cont.sensors["Near"]


    if near.positive:
        return


    parent = own.parent
    parent_mesh = parent.meshes[0]            


    # run through the vertices of the parent mesh and place blocks.       
    transform = parent.worldTransform
    for i in range(parent_mesh.getVertexArrayLength(0)):
        vertex = parent_mesh.getVertex(0,i)
        spawn_position = transform * vertex.XYZ
        place_block(own, spawn_position)


        

Thanks, but don’t you mean “Don’t use script mode”, because your script is written for module mode.

Oops! Thanks!