Copying position of GameObject still moves original object

I have learned that the position of a transform is copied, making it safe to assign the transform position of an object to another.

Below are code snippets (not the whole thing) for a player to throw an item on the ground. The item model is instantiated and the position is set to that of the player object which dropped it. Then a component is added which throws the item in an arc and it will eventually hit the ground.

Now, the item is thrown through the air, but so is the player! This suggest a linkage between the player position I copied and the item position, right? But the position is supposed to be copied?!

GameObject itemPrefab = Instantiate(item.prefabLOD);
itemPrefab.transform.position = item.owner.transform.position;
LootArc arc = itemPrefab.AddComponent<LootArc>();

I also tried this to explicitly create a new Vector3, but still same behavior:

            itemPrefab.transform.position = new Vector3(
                item.owner.transform.position.x,
                item.owner.transform.position.y,
                item.owner.transform.position.z,
            );

This is some code from the LootArc class for context (not complete):

public class LootArc : MonoBehaviour
        IEnumerator Start() {
        Vector3 pos = transform.position + velocity * Time.deltaTime;
            transform.position = pos;
            velocity += Vector3.up * gravity * Time.deltaTime;
            yield return null;
        }

        Destroy(this);
    }

I also tried setting “itemPrefab.transform.position = new Vector3(0f, 0f, 0f);” and then the player doesn’t move, so it seems related to the position copy.

What is the Transform parent relationship between the item and the player? Is the player a child object of the item? Is the Item a child object of the player? What does your scene hierarchy look like right before the item is thrown? Are you adding a LootArc component to your player object as well?

You are correct that copying the position simply creates a copy, it does not reference the old position, so something else is causing this.

Thanks for the reply!

Attached is a picture of the hierarchy when I pause while both item and player are in the air. I don’t see any obvious relationship (blue arrow is item, red arrow is player).

I am pretty new to Unity, but wouldn’t the player be a node (even leaf) under the item in the hierarchy if there was such a problematic relationship?

Looking at the player GameObject there is no LootArc component on him.

I also want to restate that if I just set the position of the item to “new Vector3(0f, 0f, 0f)” then it spawns and arcs correctly at 0, 0, 0 and the player isn’t affected. So to me it looks like something happens when the position is copied, I just can’t figure out what.

Do the player and/or the item have colliders/rigidbodies? It’s possible your item is actually colliding with and pushing your player along with it. Especially if the item is a “kinematic” rigidbody, it will easily push the player.

Thanks, that was it!

Disabled the Collider component after spawning the item and the problem went away. Will have to enable it again when it hits the ground.

1 Like

You could also do Physics.IgnoreCollisions on your player and that object temporarily.