Set Position of Child as fixed global Position

Hey there!

I got following problem:

I have “Enemies” spawning from different SpawnPoints.
Each of them has 4 Childs “PointToMove”, empty object that shall set random points different for each “Enemy” where the enemy moves to.

Now the problem is, when the enemy spawns and generates such a point he will move there, but since the childs “PointToMove” are attached to him they will move with them so he will never reach them.

I think this is a simple solved problem at all but i am pretty new to Unity and programming at all so i hope someone can help :smile:

My Script so far:

public class EnemyMovement : MonoBehaviour {

    public Transform[] point;

    public float movementspeed = 5f;
    public float xValue = 0f;
    public float yValue = 0f;

    public Rigidbody2D rb;

    private int currentPoint;


    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();

        for (int i = 0; i < point.Length; i++)
        {
            xValue = Random.Range(-3.1f, 3.1f);
            yValue = Random.Range(-1f, 5f);
            point[i].position = new Vector2(xValue, yValue);

        }

    }

    void Update ()
    {
        if(transform.position != point[currentPoint].position)
        {
            Vector2 pos = Vector2.MoveTowards(transform.position, point[currentPoint].position, movementspeed * Time.deltaTime);
            rb.MovePosition(pos);
        }
        else
        {
            currentPoint = (currentPoint + 1) % point.Length;
        }
    }
}

Short story:

How do i move a parent without moving it`s childs? :smile:

Simplest solution: Don’t make them children of the parent if they’re not meant to move with the parent.

1 Like