This page covers a bunch of code examples that I wrote myself.
All of them are written in C# focussing on the Unity 3D game engine.
Each code example is presented with a short description.
All of them are written in C# focussing on the Unity 3D game engine.
Each code example is presented with a short description.
Screen Shake
ScreenShake.cs is a class which handles Shaking the Screen!
Its a very simple thing in name, but does some small sophisticated things underneath the surface. The class is statically available as well as its main method "ShakeScreen", to increase maintainability the method accepts a Model class, instead of regular parameters. Depending on some variables set we either do a single shake or a triple shake. This results in some nice juiciness when used properly. Each time a "shake" is performed it calculates a new position to move towards based on the initialposition and a dampened max translate value. Also uses a dot product calculation to ensure the shakes are always interesting, not just bumping back and forth between the previous and new shake destinations. |
Code Editor
|
Updatable Scriptable Object
|
UpdatableScriptable.cs is a generic class which accepts any type of object as the ScriptableObject's single data type.
This type of ScriptableObject only holds one piece of data, this piece of data however can be a collection type. A ScriptableObject is Unity's way of creating an instantiated pointer towards data, available at editor time. It basically allows us to create a scriptable data object to point towards. The ScriptableObject is not a simple copy of data whenever we access it, but a pointer towards one instance, all locations that access the data, will be able to manipulate and therefore change it for all other locations. In order to update all locations of any changes to the Scriptable Object's data (internal state) changes, I created this wrapper class which holds a delegate of type Action<T> which gets raised whenever Value changes. |
Octa Directional Animator Behaviour
OctaDirectionAnimatorBehaviour.cs is a type of StateMachineBehaviour, this is a special type of class that can be added to a Mekanim State.
Mekanim is Unity's animation/statemachine functionality which enables users to easily create statemachine like behaviour for animations. I use this behaviour extensively to change between spritesheet based animations based on the SCMovementDirection. SCFowardsBackwards will be used to either play the animation forwards, or backwards. Regular AnimatorControllers don't allow the switching of Animations on states practically through code, so some workarounds had to be created. The current animationClips of the Animatorcontroller are retrieved, then the one corresponding to our state/animation is overwritten with the new animation. Then everything is reassigned and Mekanim takes over again. |
|