Tag: GitHub

Unity ScriptableObject-based Pooling Framework

Brief break from Clockwork TD devlog updates today, as I published my first Unity toolset on github! Over the past year, I’ve been using Unity’s Object Pooling feature, which is flexible and powerful, but requires a lot of boilerplate code to set up.

Around the time of Ron Swansong, I took the time to formally standardize my pooling code into a framework, which cut down my code repetition and codebase size nicely. With Accumulus, I kept tweaking and improving it, and finally after two months of development during Clockwork TD, the framework is ready to be shared with the world today.

It’s meant to be flexible but also lightweight, requiring just a few lines of code and two inheriting classes to get going. However, it allows you to override methods at every stage in the object pooling lifecycle, letting you use the framework in whatever way your project requires. Check it out and see what you think!

https://github.com/onewinter/ScriptObjPoolingFramework

2023-07-10T08:36:07-04:00July 9th, 2023|Categories: General|Tags: , , , , , |0 Comments

Simple Unity Hex Grid Framework

I’ve been using this gist in my last two projects to get an easy hex grid up and running quickly in Unity. I made some additions to it using the original source material at redblobgames.com, including an implementation of Amit’s A* Pathfinding algorithm (definitely one of my prouder comp-sci/math moments, even though I already did my own variation on A* back in Tower2Defense).

Here’s a video of the A* pathfinding in action (the enemies take damage randomly as they walk, no player towers yet in this video):

And some fun experiments with enumerating through the grid using Spiral, Ring & DistanceTo to create the game board’s pattern (the gif with enemies is actually using Unity’s AI Navigation package, which was not worth continuing to struggle with compared to the results the A* gives me now):

Finally, here’s the updated Hex.cs so you can check it out:
(Unity doesn’t include C#’s PriorityQueue class yet; you can use this direct port from the official C# lib as a drop-in replacement: https://github.com/FyiurAmron/PriorityQueue/blob/main/PriorityQueue.cs)

View this gist on GitHub

2023-05-31T05:22:15-04:00May 30th, 2023|Categories: General|Tags: , , , , , |1 Comment

Projectile Arcing in Unity with Animation Curves

I recently had to figure out how to move a projectile in an Arc for a project I’m working on and found that a Google search was harder than expected, so I’m writing this up in that hopes that it might help someone else! For starters, take a look at this post on how to do it in 2D, as well as some explanation on the math behind it. For our purposes, we’re going to let Unity handle calculating the arc by simply using an Animation Curve. (Kind of hilariously, I spent part of the past weekend learning Animation Curves in Unity, only to have it be the subject of my next Maya class come Monday morning. Hindsight!)

This is the code I’m using, which is hyper-specific to my project, but we can break it down in principle. (I’ll cover ScriptableObject TypeObjects in a later post, but that’s what’s going on here; Projectile is a MonoBehaviour and the class below is a ScriptableObject)

  • Line 7: CurrentTime += Time.deltaTime in Update(). Duration is how long the projectile should take from firing to reach its target. linearT calculates the ratio from 0 to 1 of how close to done we are, given a desired Duration.
  • Line 9: PositionXZ() is an extension function I wrote that returns a Vector3 with its Y value set to 0. This function handles moving the projectile towards the target on the X and Z axes, using Lerp and linearT to interpolate smoothly.
  • Line 11: does the same thing but only for the Y axis. This ensures that we end up at the correct height for the target, in case the start and end Y positions are different.
  • Line 13: uses the same 0 to 1 value in linearT to evaluate the animation curve and return a height offset value. This is multiplied by curveMaxHeight to determine the effective height at the top of the parabola.
  • Line 15: adds the Lerped Y to the Offset Y to get the new effective Y. (If we’re Lerping from 0y to 0y, baseY will be 0y throughout, so the arc offset will determine the Y value alone.)
View this gist on GitHub

And here’s the animation curve stored in the curveHeight field:

You’ll want to manually clamp the points on the X axis at 0 and 1 to a value of 0. This ensures that at the start and end, no height is added to the projectile. Add a key point at 0.55 and set its value to .95. Set the Start and End tangents to linear; you want the projectile to shoot up towards the peak and come down towards the target quickly. In the middle, you can keep the point at Auto; grab the tangent handle and turn it clockwise so that the right hand side of the parabola is as close to a 45 degree angle as you can get it, which should push the peak to touching 1 on the Y axis.

This system works beautifully, because the offset for the mortar in the Animation Curve matches what you expect it to look like in the game, which I find makes it easier to wrap your head around how it works. You can play around with the tangents if you want to extend that hang at the top, but this works pretty well as is.

Looks pretty good, and no tricky math required… it should also be adaptable for other projectiles like rockets or arrows if you flatten out the Animation Curve. Try it out!

Edit 2022/08/20: I randomly stumbled across https://blog.terresquall.com/2019/11/coding-projectiles-for-your-tower-defense-game-part-2/ and thought it was a great explanation of the same concept; so for further reading, check it out.

2022-10-08T04:26:59-04:00July 21st, 2022|Categories: General|Tags: , , , , , |0 Comments

GitHub & Itch.io Pages

It’s always so bizarre to open up WordPress after a long time away and see how much has changed (even moreso when you think about the fact that I’ve been using WordPress in some way or another since before it was even called that.)

Anywho, I’ve challenged myself to spend some time in between grad school semesters writing about the projects I worked on this semester and some of the stuff I’ll be working on in the future. We’ll start here with links to my GitHub and Itch.io pages, where I published the three projects I’ll be talking about in future posts.

GitHub

Itch.io

Even though this code is nothing special, I’m publishing it alongside the actual playable versions in the hope that something in there might be useful to someone down the line; in addition, I feel like it’s healthy to put your code out into the world whenever you can, inviting discussion and criticism with the eventual goal of learning and growing from the experience.

Ta for now!

2022-06-28T10:21:18-04:00June 1st, 2022|Categories: General|Tags: , , , , |0 Comments

About My Work

Phasellus non ante ac dui sagittis volutpat. Curabitur a quam nisl. Nam est elit, congue et quam id, laoreet consequat erat. Aenean porta placerat efficitur. Vestibulum et dictum massa, ac finibus turpis.

Recent Works

Recent Posts