My Enemy Healthbars are instantiating wrong

So the script that controls the billboarding of the healthbars instantiates the Canvas as a Child of the Socket, and then rotates the socket to the camera. The problem I’m having is that the canvas is being instantiated with a weird Rect Transform on the Y axis, which appears to be dependant on the rotation of the enemy.

If I try to rotate the Socket at runtime, it snaps back every frame (because of the Late Update) but if I change the Rect Transform rotation to 0 (its usually some random number like 144 or -99) then the healthbars look perfect. Prior to that its doing all kinds of weird stuff.

I’ve fiddled with the instantiation A LOT. I tried changing Quaternion.Identity to Quaternion.Eulor(0,0,0), it still instantiates with the same random rotations as Identity does. Then I tried capturing the canvas as a variable and changing the rotation of it to 0,0,0 at start, but that didn’t make any difference either.

I’m literally so confused I could punch my monitor. I assume I’m just fundamentally misunderstanding the way instantiation happens with regards to rotation but it’s really hacking me off.

Okay I fixed it with a hack by doing this:

    GameObject canvas = Instantiate(enemyCanvasPrefab, transform.position, Quaternion.identity, transform) as GameObject;
    canvas.transform.rotation = this.transform.rotation;

If I comment out the bottom line I get the bug, and I try 0,0,0 I get the bug (the socket rotation is 0,0,0) so honestly I have no idea and I assume it’s just down to Local/Global rotations which always confuse the hell out of me.

I’m gonna mark it as solved but honestly if anyone can shed some light as to what’s going on here I’d appreciate it.

1 Like

Had the same problem you did, and your code fixed it for me too, thanks!

I guess i did it using the dunkey way, i just created a new canvas an made everything my self and inputed the code files there reseting all the forms and its working perfectly :slight_smile:

I believe I had a similar issue, which was being caused by me having rotated the enemies. Quaternion.identity will align to the world or parent axes, so if a parent object is rotated, this will transfer the rotation up to this UI canvas. I solved it by simply replacing the Quaternion.identity, in the Start() of EnemyUI.cs, with transform.rotation. This gave the UI the ability to face-to the player model (comment out transform.rotation = … in LateUpdate() ), instead of the camera. Not sure if that’s useful or not, but someone may want that functionality! :smiley:

1 Like

That fixed the issue for me!

Privacy & Terms