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.