![water splash png 2d water splash png 2d](https://lh3.googleusercontent.com/-t4NEtUQHaPY/WI4kqSS66jI/AAAAAAAACFE/zD3MCDkg5Yc/s1600/PicsArt_03-29-05.54.23.png)
Now that we have our mesh, we need a function to update it as the water moves: void UpdateMeshes() We set their position to be halfway between the nodes, set their size, and add a WaterDetector class to them. Here, we're making box colliders, giving them a name so they're a bit tidier in the scene, and making them each children of the water manager again. Now we want our collider too: colliders = new GameObject() Ĭ = transform Ĭ = new Vector3(Left + Width * (i + 0.5f) / edgecount, Top - 0.5f, 0) Ĭ = new Vector3(Width / edgecount, 1, 1) Ĭolliders.GetComponent().isTrigger = true We set the mesh, and we set it to be the child of the water manager, to tidy things up. We'll start with the nodes: for (int i = 0 i ().mesh = meshes Now to actually set the values of our arrays. So now we have all our arrays, and we're holding on to our data. Now that we've made our nodes, we'll initialise all our top variables: xpositions = new float You may have noticed that SetWidth() takes two parameters these are the width at the start and the end of the line. You can vary this depending on how thick you want your line. We've set the correct number of nodes, and set the width of the line to 0.1. What we've also done here is select our material, and set it to render above the water by choosing its position in the render queue. The first thing we're going to do is render our body of water with the LineRenderer component: Body = gameObject.AddComponent() (You can vary this to balance efficiency against smoothness.) This gives us all our lines, then we need the + 1 for the extra node on the end. We're going to use five per unit width, to give us smooth motion that isn't too demanding. Now we're going to find out how many nodes we need: int edgecount = Mathf.RoundToInt(Width) * 5 (Though this seems inconsistent, it acts in the interest of quick level design when building from left to right). public void SpawnWater(float Left, float Width, float Top, float Bottom) This function will take inputs of the left side, the width, the top, and the bottom of the body of water. To do that, we'll write a function called SpawnWater(). We want a game object that can hold all of this data, act as a manager, and spawn our body of water ingame to specification. These are all going to be based on prefabs, which are all included in the source files. Plus, the kind of mesh we're going to use for the main body of water: public GameObject watermesh: Next, the material we'll use for our line renderer (in case you want to reuse the script for acid, lava, chemicals, or anything else): public Material mat: First, the particle system we're going to use for our splashes: public GameObject splash: We're going to need some public variables we can set in the editor, too.
![water splash png 2d water splash png 2d](https://officialpsds.com/imageview/79/zv/79zvqj_large.png)
These are just the dimensions of the water. Next, we're going to hold onto some values: float baseheight (You might want to change this depending on what you want to appear in front and behind of it you're going to have to use the z-coordinate to determine where sprites sit relative to it.) We're going to use -1 for this so that it gets displayed in front of our objects. These constants are the same kind as Michael discussed, with the exception of z-this is our z-offset for our water. We're also going to need colliders so that things can interact with our water: GameObject colliders Īnd we'll store all our constants as well: const float springconstant = 0.02f We're going to need objects to hold these meshes too. We still need the water itself, though we'll create this with Meshes. The LineRenderer will store all our nodes and outline our body of water. So at the top of our class we'll add these variables: float xpositions We'll have to keep track of the positions, velocities and accelerations of every node, though. We're going to render the top of our water using one of Unity's line renderers, and use so many nodes that it appears as a continuous wave. In his tutorial, Michael Hoffman demonstrated how we can model the surface of water with a row of springs. You'll need the Unity browser plugin to try it out.Ĭlick to create a new object to drop into the water.
#Water splash png 2d how to#