Camera rotation, lifebar and billboard


This week  I added a control that rotates the camera around the selected unit.  This was a feature I didn't think I would need as my plan was to lock the camera. When I started adding some objects like trees to the level I quickly realised that locking the camera wasn't an option, unless I started to do some sort of transparency on the objects when a character was behind them. I don't discard this but for now I decided to go for the camera rotation approach.

The process was quite simple although had some knock on effects. First I added a small button image using the UI editor.  This editor is great and is very straight forward and there is a handy feature on it and that is that you can assign an event to an element that gets trigger when a user clicks in that element. The only problem is that  you cannot control the action, i.e. down, release, etc like you would do using logic nodes or from code. 

My rotate button should work by rotating the camera when the button is pressed down, I addressed this quite simply by creating a Trait that basically detects if the mouse is down and if its coordinates are within the button boundaries.

var x = Input.getMouse().x;
var y = Input.getMouse().y;
if (Input.getMouse().down() && 
   x >= canvas.getElement("rotate").x && x <= canvas.getElement("rotate").x + canvas.getElement("rotate").width &&
   y >= canvas.getElement("rotate").y && y <= canvas.getElement("rotate").y + canvas.getElement("rotate").height) {
   this.object.transform.rotate(new Vec4(0, 0, 1), 0.017);    
}

Once the above was done, the next problem were the lifebars. In my previous post GIF you can see that they were just a low poly cylinder.  Now with camera rotation enabled I needed to make sure that they were facing the camera all the time.  This could have been fixed quickly by updating  the Trait that updated the lifebar location, however I wasn't happy with the implementation of lifebars in general, each lifebar was an object on my project in Blender, so I decided to use a plane using a material set to billboard that will be spawned on runtime:

Spawning objects during runtime is handy and you don't have the restriction of one object one name that you have in Blender, and there was no need to maintain multiple identical objects on the project when one should be enough.


Billboard setting works as expected however I encountered a bug, I cannot change the scale of the object when set to billboard. I am hoping it gets fixed soon because when a character gets hit, apart from changing the color from green to red, the bar should decrease in size. But again, this is not a biggie and if it doesn't get fixed soon a simple trait would do the trick in the meantime.

Leave a comment

Log in with itch.io to leave a comment.