Category Archives: Unity3d

Legacy Animations and Looping

Quick note to self…

When using legacy animations in Unity, make sure you set them to loop (if that’s what you need), otherwise they play once & stop.  Spent more time trying to figure this out than I’d like to admit.

Pretty simple to do:

	animationController = GetComponentInChildren();
	animationController.wrapMode = WrapMode.Loop;

You might want to check that animationController isn’t NULL before you update wrapMode.

Rigidbody, Collisions & Unity3D

Working on a new game app, and I wanted to know when my player object collided with a crate.  Thought I did everything right, but it turns out I was missing on critical thing.

Screen Shot 2015-06-06 at 8.29.49 PMMy player object had a rigidbody, neither gravity or kinetic were checked.  I added a box collider and scaled it to enclose my 3D player object.

The crates had a box collider with trigger checked, but no rigidbody.  My thought here was as I had one player and upwards of 60 crates in the scene, I could get away with one rigidbody instead of 60 which would save memory and maybe some execution time without any drawbacks.

The game has the crates moving towards the player and the player doesnt move (at the moment).

Turns out when you do this, the rigidbody attached to the player will ‘fall asleep’ and not detect any collisions.  Took me quite some time to figure that out!  To correct it, you could add a zero force to the rigidbody, or just call the WakeUp() method.  I chose the later method by adding the following two lines in my player’s Update method:

		if (rigidbody.IsSleeping())
			rigidbody.WakeUp();

That solved my problem, and my player now generated OnTriggerEnter calls!  Hopefully this post will help save someone else from the same hair-pulling exercise!

Gameplay Updated Screenshot

As you can see from the ‘before & after’ image below, the in-game visual style got quite a lift from the old ‘programmer’ art. I really like the textured background. This is done with a tiled sprite repeated horizontally. The color tint is adjusted based on the current zone.

gamePlayCompare

Preserve Code Plugin Test

Just installed a plugin that declares that it will preserve code formatting… let’s see how well it works with some shader programming…


Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord2 : TEXCOORD1;
fixed4 color : COLOR;
};

struct v2f
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord2 : TEXCOORD1;
fixed4 color : COLOR;
};

sampler2D _MainTex;
sampler2D _DetailTex;
float4 _MainTex_ST;
float4 _DetailTex_ST;
float4 _DetailTex_TexelSize;
fixed4 _Color;
fixed _Strength;

v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
o.texcoord2 = TRANSFORM_TEX(v.texcoord2 * _DetailTex_TexelSize.xy, _DetailTex);
o.color = v.color;
return o;
}

half4 frag (v2f i) : COLOR
{
half4 col = i.color;
fixed4 detail = tex2D(_DetailTex, i.texcoord2);
col.rgb = lerp(col.rgb, col.rgb * detail.rgb, detail.a * _Strength);
col.a *= tex2D(_MainTex, i.texcoord).a;
col = col * _Color;
clip (col.a - 0.01);
return col;
}
ENDCG
}

Seems to work pretty well, but I ended up editing the HTML & pasting the code in that view. I’ll see how it goes over the next few weeks.

64 bit iOS required

As of today, all apps submitted to the App store, including updates need to support 64bit.  Got a bit of a shock when I tried submitting my build of Candy Bubble Drop and saw this error screen!

Screen Shot 2015-06-01 at 3.46.54 PM

Luckily, the folks at Unity were prepared (more than I was) and I was able to enable 64 bit support from within the Player Settings and everything worked beautifully!