Space Shooter Unity tutorial - Child Entity location doesn't follow parent

I’m making good progress. I now have enemy ships spawning with the asteroid hazards. For some reason that I can’t quite figure out, the shotSpawn location of the enemy ships doesn’t seem to be updating relative to the ships location. Any help is appreciated. I also welcome your feedback on any other aspects of my code and use of the game engine.

https://playcanvas.com/project/517158/overview/space-shooter-tutorial

Ooo. Looks like a bug with entity attributes perhaps? If I change this:

WeaponController.prototype.fireWeapon = function() {
    const self = this;    
    const newShot = this.shot.clone();
    console.log('fireWeapon:' + self.shotSpawn.getPosition());
    newShot.setPosition(self.shotSpawn.getPosition());
    newShot.setRotation(self.shotSpawn.getRotation());
    newShot.enabled = true;
    this.entity.sound.play('Shot');  
};

To this:

WeaponController.prototype.fireWeapon = function() {
    const self = this;    
    self.shotSpawn = this.entity.findByName('Shot Spawn');  // Added this line!
    const newShot = this.shot.clone();
    console.log('fireWeapon:' + self.shotSpawn.getPosition());
    newShot.setPosition(self.shotSpawn.getPosition());
    newShot.setRotation(self.shotSpawn.getRotation());
    newShot.enabled = true;
    this.entity.sound.play('Shot');  
};

…then it works.

1 Like

Hmmm actually, maybe it’s not a bug. The cloned ship is still pointing to the ‘prefab’ shot spawn. Not the cloned child. Hey, @vaios, what do you think about this? Personally, I don’t see how the current behavior is desired. I guess it should be:

If entity script attribute points to a child entity in a clone operation, point to the cloned child. Otherwise, point to the non-child original.

1 Like

Thanks for the workaround. Although it would be nice if we could get the expected behavior you describe:

I’ve implemented your fix but I noticed a weird bug. Even though it looks like enemy shots are now instantiated properly and in the right location, there seems to be some kind of a weird invisible “ghost” instance of every shot that is instantiated along with each shot, in the wrong location. To reproduce the bug, simply start the game and don’t move the player ship. You will notice that as soon as an enemy ship appears on screen and fires a shot, the player ship will blow up. As if the enemy shot was instantiated right on top of the player ship’s position.