# how can I lock position to a child

I made for a space game my spaceship to create some particles when it moves but the spot where they instantiate moves slower than the spaceship causing it to follow up and not to lock on the space ship. Is there any way I can make the children gameobjects move exactly like how spaceship moves?.[138467-στιγμιοτυπο-οθονης-17.png|138467]

Here is the script:

public class playerMovement : MonoBehaviour
{
private Rigidbody2D rb2d;
private playerStats PlayerStats;
private float playerInput;
public ParticleSystem particles;
private Transform LeftRocket;
private Transform RightRocket;

void Start()
{
LeftRocket = GameObject.Find("LeftBoost").GetComponent<Transform>();
RightRocket = GameObject.Find("RightBoost").GetComponent<Transform>();
PlayerStats = gameObject.GetComponent<playerStats>();
rb2d = gameObject.GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.RightArrow)&& playerInput >0)
{
Instantiate(particles, LeftRocket.position, LeftRocket.rotation);
}
else if(Input.GetKey(KeyCode.LeftArrow) && playerInput < 0)
{
Instantiate(particles, RightRocket.position, RightRocket.rotation);
}
playerInput = Input.GetAxis("Horizontal");
rb2d.velocity = new Vector2(playerInput * PlayerStats.Speed, rb2d.velocity.y);
if (Input.GetKey(KeyCode.RightArrow) && Input.GetKey(KeyCode.LeftArrow))
{
rb2d.constraints = RigidbodyConstraints2D.FreezePositionX;
}
else
{
rb2d.constraints = RigidbodyConstraints2D.None;
}
}
}

The position and rotation of a child object is inherited from its parent automatically. Perhabs the particle system configuration is were you should look for a problem.
Are you using any script referencing to this child or particle changing transform values?

There may be a better way but have you thought about removing it from being a child when manipulating it’s parent and then making it a child again after?

I missed something this last method, will instantiate always on key press multiple times, here is another way that makes them on start a child and activates the particles only on key press.
Here is version 2.0:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rocketController : MonoBehaviour {
private Rigidbody2D rb2d;
private float playerInput;
public ParticleSystem particles;
private Transform LeftRocket;
private Transform RightRocket;

void Start()
{
LeftRocket = GameObject.Find("LeftBoost").GetComponent<Transform>();
RightRocket = GameObject.Find("RightBoost").GetComponent<Transform>();
rb2d = gameObject.GetComponent<Rigidbody2D>();

Instantiate(particles, LeftRocket.position, LeftRocket.rotation, LeftRocket);
LeftRocket.transform.Find("Particle System(Clone)").gameObject.GetComponent<ParticleSystem>().enableEmission = false;
Instantiate(particles, RightRocket.position, RightRocket.rotation, RightRocket);
RightRocket.transform.Find("Particle System(Clone)").gameObject.GetComponent<ParticleSystem>().enableEmission = false;
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.RightArrow)&& playerInput > 0){
RightRocket.transform.Find("Particle System(Clone)").gameObject.GetComponent<ParticleSystem>().enableEmission = true;
}
else if(Input.GetKey(KeyCode.LeftArrow) && playerInput < 0){
LeftRocket.transform.Find("Particle System(Clone)").gameObject.GetComponent<ParticleSystem>().enableEmission = true;
}

if (Input.GetKeyUp(KeyCode.RightArrow)){
RightRocket.transform.Find("Particle System(Clone)").gameObject.GetComponent<ParticleSystem>().enableEmission = false;
}
else if(Input.GetKeyUp(KeyCode.LeftArrow)){
LeftRocket.transform.Find("Particle System(Clone)").gameObject.GetComponent<ParticleSystem>().enableEmission = false;
}

playerInput = Input.GetAxis("Horizontal");
rb2d.velocity = new Vector2(playerInput * 1, rb2d.velocity.y);
if (Input.GetKey(KeyCode.RightArrow) && Input.GetKey(KeyCode.LeftArrow))
{
rb2d.constraints = RigidbodyConstraints2D.FreezePositionX;
}
else
{
rb2d.constraints = RigidbodyConstraints2D.None;
}
}
}