Search Unity

Catlike Coding's C# and Shader Text Tutorials

Discussion in 'Community Learning & Teaching' started by Jasper-Flick, Jul 31, 2011.

  1. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Last edited: May 20, 2014
  2. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    I published a new tutorial. This is the first one that has been supported via Patreon!



    Constructing a Fractal, using recursive logic, coroutines, and randomness
    Use recursion, a coroutine, and randomization to grow your own 3D fractal.
     
    Last edited: Sep 30, 2014
  3. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    I'm working on my next tutorial. It's not written yet, but you can already try the concept web player.



    Maze, creating confusing office space

    I'll include a first-person mode as well, if my 4th Patreon goal is reached before the end of the month. So if you like to help, become a patron today!
     
    Last edited: Jun 25, 2014
  4. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Maze, my second Patreon-supported tutorial, is now done!



    Maze, building your own randomized place
    Generate a maze with different room types, then walk around in it.
     
    Last edited: Sep 30, 2014
    Zac439 and yasirkula like this.
  5. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Here is my third Patreon-supported tutorial!



    Curves and Splines, making your own path
    This tutorial will take you from creating a simple line all the way to editing your own Beziér splines.
     
    Last edited: Sep 30, 2014
    Zac439 likes this.
  6. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Last edited: Sep 30, 2014
    Zac439 likes this.
  7. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    There's a new tutorial that is about analytical noise derivatives and neat things you can do with them.

     
    Zac439 likes this.
  8. Matthew-Schell

    Matthew-Schell

    Joined:
    Oct 1, 2014
    Posts:
    238
    These are great Jasper, some really interesting ideas here. Thanks for sharing!
     
  9. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
  10. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Here is a new tutorial! It has been commissioned by Binpress, so it's published on their website.

    Octahedron Sphere

    Create an approximation of a sphere by subdividing the faces of an octahedron. This tutorial was commissioned by Binpress.
     
  11. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Following the previous two noise tutorials, I have written about Simplex noise.

    Simplex Noise
    Discover how to create alternatives for Perlin and Value noise, based on a simplex grid.
     
  12. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    I've started a new tutorial series about Marching Squares. Here's the first one.

    Marching Squares
    Build an editable 2D voxel grid, then triangulate it with the Marching Squares algorithm.
     
    Zac439 likes this.
  13. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    The second Marching Squares tutorial is now online!

    Marching Squares 2
    Add vertex sharing and flexible edge crossings to Marching Squares.
     
    Zac439 likes this.
  14. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    And here's the third part of the Marching Squares tutorial series!

    Marching Squares 3
    Enhance Marching Squares with Hermite data, then reconstruct sharp features and resolve ambiguous cases.
     
  15. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    On to the fourth part!

    Marching Squares 4
    Refactor Marching Squares a bit, then add walls to give it some depth.
     
  16. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    The fifth and final part of the Marching Squares series is now available.

    Marching Squares 5
    Make Marching Squares more colorful by adding support for multiple materials.
     
  17. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Swirly Pipes is my first tutorial specifically for Unity 5. It can be considered the spiritual successor of the old Runner tutorial.

    Swirly Pipe
    Create a prototype racing game where you move through endlessly twisting pipes.
     
  18. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    You can now also try Swirly Pipe on your iOS device. Touch input delivers quite a different experience compared to desktop.
     
  19. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Change of approach! From now on I'll release more frequent, smaller tutorials.

    This time it's a dedicated tutorial about the basics of meshes.

    Procedural Grid
    Create a simple grid of vertices and triangles.
     
  20. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Not as soon as I hoped, but here's the next one!

    Rounded Cube
    Use your knowledge of grids to build a rounded cube.
     
  21. Rick Love

    Rick Love

    Joined:
    Oct 23, 2014
    Posts:
    76
    @Jasper Flick

    It's seldom I find tutorials that genuinely impress me. Well done!

    Your explanations, demo animations, and organization make it enjoyable to read and digest.
     
  22. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Thanks @Rick Love! It's good to hear that my tutorials are useful.
     
  23. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    Really enjoying the vertex manipulation tutorials. Very cool. Thanks for sharing! :)
     
  24. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
  25. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    This time it's all about math!

    Cube Sphere
    Turn a cube mesh into a sphere, then use mathematical reasoning to improve it.
     
  26. gyro_gearloose

    gyro_gearloose

    Joined:
    Jun 11, 2015
    Posts:
    11
    Great tutorials. They are great to work on if you can only put an hour or two in at any one time. One thing I would change though is where your variable names are often the same as their type, or method. For example, from the Fractal tutorial:
    Code (CSharp):
    1.     public Mesh mesh;
    2.     public Material material;
    3.    
    4.     private void Start () {
    5.         gameObject.AddComponent<MeshFilter>().mesh = mesh;
    6.         gameObject.AddComponent<MeshRenderer>().material = material;
    7.     }
    Too many meshes and materials! This might be a bit clearer:
    Code (CSharp):
    1.     public Mesh fractalMesh;
    2.     public Material fractalMaterial;
    3.    
    4.     private void Start () {
    5.         gameObject.AddComponent<MeshFilter>().mesh = fractalMesh;
    6.         gameObject.AddComponent<MeshRenderer>().material = fractalMaterial;
    7.     }
     
  27. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    The context of a variable or field gives you all the information that you need. In the context of the fractal, when we talk about a mesh, it is the fractal's mesh. And if we access the mesh of a MeshFilter, it is the MeshFilter's mesh. It is beneficial to get good at parsing these contexts.

    If we were to explicitly mention context in the field names themselves, then MeshFilter would have a meshFilterMesh instead of just a mesh, and a MeshRenderer would have a meshRendererMaterial instead of just a material.
     
  28. gyro_gearloose

    gyro_gearloose

    Joined:
    Jun 11, 2015
    Posts:
    11
    If thats the case, why not call 'mesh' 'a', and 'material' 'b'? By your argument those variable names are just as valid because everything you need to know about them can be derived from how and where they are used.

    Calling 'mesh' 'fractalMesh' makes sense because it is the mesh we want to build our fractal with. The same goes for 'material'. It makes each line of code more readable because one does not need to refer back to other parts of the code in order to determine what those variables are for.
     
  29. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    The context of that quote is about prepending stuff to a sane variable name; comparing "mesh" with "fractalMesh", not "mesh" with "a". Single-letter names can be perfectly fine, but usually not as field names for complex types.

    That something makes sense doesn't mean that it is necessary. When looking at code, you must know its context. Which class? Which method? Usage of a mesh field inside the Start method of some fractal class tells me all I need, provided the code is structured well. I do the same with speech. If I were to talk with someone at length about the fractal class, I would just talk about its mesh. I wouldn't explicitly say "the fractal's mesh" each time. And if the other person is paying attention, they will understand.

    But that's just me. If many people had trouble with my approach, I might change it; but this is not the case. Of course you're free to code in your own style; that's much better than copying my code verbatim.
     
  30. gyro_gearloose

    gyro_gearloose

    Joined:
    Jun 11, 2015
    Posts:
    11
    In your code you have a variable 'mesh', of a the type 'Mesh', which gets assigned to another variable also called 'mesh'. Do you not see how this might be a little confusing, especially to someone learning to code? And why is having a variable name the same as its own type a good idea?
     
  31. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Are you personally confused, or are you speaking for a hypothetical newbie? I won't change my ways, but I can always add clarifications to tutorials when needed.

    The name and type of a variable are two separate things. The type has no control over the name. Without context, it is neither bad nor good.
     
  32. gyro_gearloose

    gyro_gearloose

    Joined:
    Jun 11, 2015
    Posts:
    11
    No, I'm not confused. Far from it. I have a degree in computing, and have programmed in Basic, Pascal, Fortran, C, C++, and assembly for Z80, Intel, Motorola, and Atmel procesors, so I find your tutorial straight forward and easy to follow. The point I'm making, and one which you seem intent on ignoring, is that your naming conventions go against what is considered best practice, or at least what I was taught as best practice.

    All programming courses I've taken stress the importance of clear, readable code, which includes how one names ones variables. I can tell you that if I'd made an int called int, for example, or a Mesh called mesh I would have lost marks on my coursework.

    I'm well aware of that, but can you really not see that having a variable named the same as its type is bad practice?
     
  33. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    You didn't make your point explicit until now. I go against what you were taught, apparently. Yet my code appears straightforward and easy to follow.
     
  34. gyro_gearloose

    gyro_gearloose

    Joined:
    Jun 11, 2015
    Posts:
    11
    Yes, you're right. I should have pointed out the difference earlier between how you write code and how I was taught to write code.
    Oh for goodness sake! It makes sense to me because I have experience as a programmer and because your code is quite short, not because of the way you name your variables. I imagine that your style of naming variables would make things un-necessarily harder to follow on a larger project.
     
  35. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Noted. End of discussion.
     
  36. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    This time we're smashing nucleons together and measure our frame rate while we do it.

    Frames Per Second
    Design a test scene to tank your FPS, then measure it with your own counter.
     
  37. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    While I do think these tutorials are great and unique, I admittedly have been coming back to this thread to watch the gyro-Jasper argument. :)
     
  38. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Here's the next tutorial! I had to cover the object pools subject at some point.

    Object Pools
    Create a fountain of stuff, then feed it with your own object pools.
     
  39. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Here's a new one.

    Mesh Deformation
    Take any mesh and turn it into a deformable stress ball.
     
  40. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    I have published the first part of a new tutorial series! It is a series about hexagon maps. Lots of strategy games use them.

    Hex Map, part 1
    Creating a Hexagonal Grid

     
    Last edited: May 8, 2017
    Socrates likes this.
  41. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    The second hex map tutorial is now available.

    Hex Map, part 2
    Blending Cell Colors

     
    Last edited: May 8, 2017
  42. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    I'm now also producing a tutorial series about rendering.

    Rendering, part 1
    Use matrices to transform space.

     
    Socrates likes this.
  43. Cainebog

    Cainebog

    Joined:
    Jul 15, 2015
    Posts:
    8
    Hi there, having fun with your tutorials as always. Ran into a problem here, the script would not create the rigid body component, had to add it manually through the editor to both prefabs, any idea why that might have happened?
     
  44. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    @Cainebog The RequireComponent attribute makes sure that the required component is added when you attach this component to an object. However, if you add the attribute later it won't retroactively enforce it. So you might've added the component before you included the RequireComponent attribute (or saved the script only later). In that case, you'd have to add the rigidbodies manually, or delete and reattach the component.
     
  45. Cainebog

    Cainebog

    Joined:
    Jul 15, 2015
    Posts:
    8
    Thanks for the feedback, Working through another of your tutorials, The object fountain. Proving tricky as well. Thanks for the help.
     
  46. Cainebog

    Cainebog

    Joined:
    Jul 15, 2015
    Posts:
    8
    Hi Jasper,

    Coming unstuck quickly on this one, was fine with previous particle generator tutorial but quite soon into this one had issues with the following error and am a little puzzled.

    IndexOutOfRangeException: Array index is out of range.
    StuffSpawner.SpawnStuff () (at Assets/StuffSpawner.cs:23)
    StuffSpawner.FixedUpdate () (at Assets/StuffSpawner.cs:17)

    here is the code from relevant script, not sure where the problem is.



    using UnityEngine;

    public class StuffSpawner : MonoBehaviour {

    public float timeBetweenSpawns;

    public Stuff[] stuffPrefabs;

    float timeSinceLastSpawn;

    public float velocity;

    void FixedUpdate () {
    timeSinceLastSpawn += Time.deltaTime;
    if (timeSinceLastSpawn >= timeBetweenSpawns) {
    timeSinceLastSpawn -= timeBetweenSpawns;
    SpawnStuff(); //this is line 17
    }
    }

    void SpawnStuff() {

    Stuff prefab = stuffPrefabs[Random.Range(0, stuffPrefabs.Length)]; //line 23
    Stuff spawn = Instantiate<Stuff>(prefab);
    spawn.transform.localPosition = transform.position;
    spawn.Body.velocity = transform.up * velocity;
    }
    }

    Any insights would be appreciated.
     
  47. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    @Cainebog You have to put the prefabs into the stuffPrefabs array, via the inspector. Just like with the nucleons. Otherwise, there isn't anything to spawn. See the relevant screenshot in the tutorial. I now mention this explicitly in the text as well.
     
  48. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Another Hex Map installment is now available!

    Hex Map, part 3
    Give cells different elevation levens and connect them with terraces.

     
  49. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
  50. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    Last edited: May 24, 2016