Transform position of child is way off.
Hi. This is my code
public GameObject prefabHealthBar;
private void Start()
{
GameObject HealthBar = Instantiate(prefabHealthBar) as GameObject;
HealthBar.transform.parent = transform;
HealthBar.transform.position = new Vector3(0,1,0);
}
so I have a prefab of a healthbar that I want to follow my enemies above their head.
in my world I have enemies placed alreday. Lets take Enemy1.
It has transform pos of X= 5, Y= 0, Z= 0.
Now when I press play and the healthbar spawns in from code it should be at transform 0,1,0 (just above the enemy head, problem is my healthbar spawns with a
transform pos of X= -5, Y= 1, Z= 0.
This makes the healthbar appear way to the side of my enemy. How can I fix this?
Answer by LudvigJensen · Aug 17, 2021 at 08:44 PM
Found the solution to this. All I did was change this code
HealthBar.transform.position = new Vector3(0, 1, 0);
HealthBar.transform.localPosition = new Vector3(0, 1, 0);
to the second version. :D
Your answer
Follow this Question
Related Questions
Instantiate not spawning at correct position 1 Answer
Pool Instantiate 1 Answer
RPG instantiate problem 3 Answers
Instantiate at touch.position 0 Answers
Small Gap between endless cubes 1 Answer