Friday, October 3, 2014

Prototype - Water Droplet

A lot has happened in our prototype since my last post in preparation for our game pitch to a panel of industry experts.

I've been busy building a level showing a puzzle potential with our game mechanic with having water droplets drop into buckets in the past to have the buckets rust and open up new areas in the future.

In this post I'll talk about one mechanic I put together for water droplets. I didn't have time to do some kind of splash animation when the droplets collided with something, but I wanted to have something happen more satisfying than simply disappearing.

First things first, I made a water generator object, and put it where I wanted the water to spawn.


Then off the screen, I added a droplet sprite based off an image I found online, and set the RigidBody2D gravity scale to 0 on it to make sure it didn't move anywhere during the game.

Now I needed the water generator to periodically spawn a drop of water, to produce a leaky faucet effect. To do this I attached a script to the water generator and using my brilliant intellect, I named the script "WaterGenerator."



This script uses the update method to check how much time has gone by and spawns whatever sprite you specify at the location of the generator using the Instantiate method. It then stores the instance of the spawned sprite, although for my purposes I don't think I ended up needing to do that and could probably get rid of that collection here.

The last line also sets the gravityScale to 1 on the water droplet sprite to make sure it falls after being created.

So far so good! So the water falls, and with a Polygon Collider 2D it collides with objects... but that's not really what I wanted.

I could have the water droplet be destroyed whenever it collides with an object but that really isn't satisfying enough for the prototype. I didn't have time to figure out how to do a fancy splash animation either.

So instead I decided to add a script to the water droplet to have it shrink for a few seconds when colliding with something until it disappears.

First the class variables:



    public class WaterCollision : MonoBehaviour {

 public float disappearTime = 1.0f;
 private float scaleMultiplier = 1.1f;
 private bool started = false;
I wanted to readily have control over the amount of time it took for the water to disappear, so I made disappearTime public. The scaleMultiplier adjusts how quickly the water will shrink, and the started variable allows me to keep track of two states of the water droplet before and after it begins it's path to death!

After experimenting a bit with OnCollisionEnter and OnCollisionStay, OnCollisionEnter seemed to be the place that consistently did what I want, although there were some occasions where my water droplet wouldn't fully destory and that probably could have been fixed by adding code in both places.


    public class WaterCollision : MonoBehaviour {

 void OnCollisionEnter2D(Collision2D collision)
 {
  if (!started) {
  
   disappearTime = Time.time + disappearTime;
   started = true;
  }
  StartDestroying ();
 }

This is pretty straight forward, if the object hasn't already started to be destroyed, just set the time you want it to be destroyed and then begin shrinking the thing.

Here is my StartDestroying method:


    public class WaterCollision : MonoBehaviour {

 private void StartDestroying()
 {
  gameObject.transform.localScale = gameObject.transform.localScale/scaleMultiplier;
  
  if (Time.time > disappearTime) 
  {
   Destroy (gameObject);
  }
 }

In the interest of time I made this as simple as possible, just scale the droplet down by dividing by a value greater than 1, then check if enough time has passed to destroy the object.



No comments:

Post a Comment