Search Unity

Absolute noob here, asking for very basic pointers on unity api

Discussion in 'Getting Started' started by kiunindustries, May 25, 2019.

  1. kiunindustries

    kiunindustries

    Joined:
    May 14, 2019
    Posts:
    3
    Hi,i'm an absolute noob and I'm trying to learn c# and ultimately game development in Unity. I've covered the basic syntax in C#, and still very new. At the same time I read up on Unity api now and then to check whether I've reach the point where I can understand the code there, and that's when I suppose I can start learning Unity api. Can some one please help me out here by commenting on this code in Unity api, explaining which part is a method being called, which are the parameters, or which part is the variable, and which are the properties, or where is a class being called, etc? I'm confused because what I have learned for C# is first you instantiate a class using Main(string[]args) and then create the object with = new (); and call the member method using <instantiatedobject_identifier><dot><method1_identifier>(); and finish off with console.readline. But these lines of unity api code seems to skip a few steps.

    Also, at the same time, can you also recommend me a book or website which does this, i.e. explain each element of the Unity api line by line in terms of what is being done?

    ...

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Example : MonoBehaviour
    4. {
    5.    CharacterController controller;
    6.    private void Start()
    7.   {
    8.      controller = GetComponent<CharacterController>();
    9.      controller.center = new Vector3(0, 1, 0);
    10.    }
    11. }
    ...
     
    Last edited: Jun 12, 2019
    JoeStrout likes this.
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Hello, welcome to the Unity community, and props to you for asking a clear, coherent question! My only suggestion is that when you paste code here, you need to use the "insert code" button in the editing toolbar, which looks like this:
    upload_2019-5-25_6-34-34.png

    So those generic C# references you've been reading have you create a "main program" — a place for the computer to begin when it launches your app. That's what Main(string[] args) is. In Unity, you don't have that; Unity itself is the main program, and it knows how to launch. So forget about that.

    Also, while you can create a plain old C# class and instantiate it in Unity, that's not usually what you will be doing, especially as a beginner. Instead you will be creating subclasses of a built-in class called MonoBehaviour (apparently some of the early Unity devs were British, though the API is not consistent about this). And you can't create a MonoBehaviour (or any subclass thereof) instance with new, for reasons you will understand later. For now just take it as a fact.

    So how, then, does a MonoBehaviour existence come into being? Again this is something Unity does automatically. More specifically: you attach such a script to a GameObject in your scene (i.e. in the Hierarchy tab in Unity). And then when that scene is loaded, Unity automatically creates an instance of it, attached to that object.

    The reason you derive your classes from MonoBehaviour is that this special built-in class has a lot of functionality that ties into the whole Unity system. In particular, it has a number of magic methods that get automatically invoked at the appropriate times:
    • void Awake() gets invoked when the object is created.
    • void Start() gets invoked slightly later, before the first frame after the object is created (but after all objects have been called with Awake).
    • void Update() gets called on every frame of the game. If your game is running at 100 fps, then every object in the scene gets its Update methods called 100 times per second.
    There are others, but these three are the key ones you should focus on for now.

    So now you should be able to understand better what's going on here.
    Code (CSharp):
    1. // the "using" clause lets you access UnityEngine stuff
    2. // without prefixing it with "UnityEngine." all the time:
    3. using UnityEngine;
    4.  
    5. // Here's a class called Example, that subclasses MonoBehaviour
    6. public class Example : MonoBehaviour {
    7.     // declaring a class field called "controller" of type "CharacterController"
    8.     // (which is really UnityEngine.CharacterController)
    9.     CharacterController controller;
    10.  
    11.     // look, we're implementing one of the three magic methods!
    12.     private void Start() {
    13.         // GetComponent finds another component on the same
    14.         // GameObject as this script, of the indicated type
    15.         controller = GetComponent<CharacterController>();
    16.  
    17.         // And here we're setting the "center" property of that.
    18.         controller.center = new Vector3(0, 1, 0);
    19.     }
    20. }
    I reckon you are very close to a breakthrough in understanding, so keep at it, and ask lots of questions!
     
    Siegewolf and Ryiah like this.
  3. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    You might want to find a starter tutorial which will help you a lot in understanding the code.
     
  4. kiunindustries

    kiunindustries

    Joined:
    May 14, 2019
    Posts:
    3
    Hi JoeStrout, thank you very much! What you have written really helps alot. I have been putting a lot of hours in C#. And I have found many good books and resources, like O'Reilly's C# Pocket Reference, Microsoft's own C# documentation, and now I'm able to write simple programs, like, simple chatbots using Console.ReadLine() etc. But its still a bit slow-going. So I'll have to ask you some more questions hoping it'll clear my doubts and propel me faster ahead.

    Firstly, thank you for clearing most doubts about that example chunk of Unity code, but there is one line I still can't wrap my head around.

    Code (CSharp):
    1.  controller.center = new Vector3(0, 1, 0);
    I mean, normally you use " = new" to create a new instance of a class, and the syntax is usually "Rectangle R = new Rectangle" in which R is a new object, so the whole statement means R will be the new Rectangle. Yet what does controller.center = new Vector3() means? The dot shows that the variable/method center is accessed out of controller. If its accessed from another class than how can it be the new instanced object of Vector3()? Is Vector3() a struct? So its different altogether? Yet I saw that the syntax for declaring new instance of a struct is the same as that for class.
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Vector3 is indeed a struct, but you're right, the creation of a struct is no different from the creation of a class. So let's unpack this statement. An assignment statement always evaluates the right-hand side (after the '=') first, and then stuffs it into whatever is referred to by the left-hand side (before the '=').

    So, this creates a new Vector3, initialized to x=0, y=1, z=0. And then it stuffs it into the field referred to by controller.center.

    Assigning to a field of another object is no different from assigning to a local variable, or a field of the class you're in, or whatever. As long as the compiler can work out what you're trying to assign to... and the permissions on that field allow other code to assign to it... then the assignment will be done.

    Does that clear it up?
     
  6. kiunindustries

    kiunindustries

    Joined:
    May 14, 2019
    Posts:
    3


    Words cannot express my gratitude for your guidance. I have now roughly covered C# and understood how it relates to unityengine. But it seems, to design special functionalities(apart from moving characters, spawning objects, moving background etc) in code from scratch, mastering the unity api involves a steep learning curve. So despite all my knowledge in C#, I still can't avoid copying and pasting code from whatever source I can find, so I will do that for now, in hopes that as I go along learning more I'll be able to get creative in using code. But I'm glad at least I know now where to paste the codes, tweak some things here and there to custom things to my need, or correct errors that might throw up.

    Now I'm at the stage of learning how to manipulate the objects in order to produce the effect I need. Particularly troubling me right now is the choice between unity3D and 2D. I only want to produce 2D games, but it seems many 2D games (the more "high-quality" ones) are not really merely 2D, but employ 2.5D(in various senses of the term). For example, "Ori and the blind forest", and "Hollow Knight". Could I have your advice again on this aspect?

    I've learnt that Ori is a sprite created from the animation of a 3D model. Is it 3D sprite or a 2D sprite? If its a 2D sprite I don't quite understand the efficiency of this. I mean, why not just draw the various frames of the 2D sprite animation? Ori is a silouette anyhow, so no benefit is reaped from the accuracy of 3D lighting. Perhaps just so to make the movement more fluid? Or is it actually a 3D sprite? I suppose a sprite is by definition 2D, but what I mean is, is Ori an animated 3D object?

    My second related question, have you ever played this game "Kaiju Rush"? Its a smasher game, and the game play involves a monster being sprung into the air across a city. And as it's in the air it gets transform into a ball, and tapping on the screen presses the ball downwards, smashing buildings underneath. As the ball is in the air, there is a circle beneath it, representing a target marker, which not only follows the ball but enlarges and shrinks in perspective according to the horizontal movement of the ball. Obviously the game is a 2D game, since it's rendered in pixel graphics. But is the ball a 2D sprite or an actual 3D sphere with 2D texture? Seems more like a 2D sprite. But what is the mechanism that portrays the movement of the ball and also, the collateral movement of the target marker? Both of the items are one object, and different sprites portraying the relationship of the ball and the target marker are called on at different events, or they are two objects?


    WhatsApp Image 2019-07-08 at 7.55.52 PM (1).jpeg WhatsApp Image 2019-07-08 at 7.55.52 PM.jpeg

    After the ball loses all momentum it will transform back into a monster. My other question is about the relation between the ball/monster and the buildings - the game would portray the position of the ball/monster appropriately when they are behind a building. If all of this is in 2D then the buildings would probably be individual isometric sprites? But how is the ball/monster made to move before and after the building sprite layer as and when its appropriate? The There is a collider at the base of each of the buildings? Seems like this might be quite tedious. Could everything be in 3D but made to look like 2D?

    WhatsApp Image 2019-07-08 at 8.34.57 PM.jpeg

    Sorry if my questions are too many. You can answer as much/little to your convenience. Thank you very very much.
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I don't really know any of those games. You provided some nice screenshots of Kaiju Rush though, so my guess would be that these are ordinary 2D sprites, layered according to their Y position. This is a standard technique for producing "2.5D" (isometric) graphics.

    It's true that Unity is an awful lot to learn at first. I've spent a fair amount of time teaching people (mostly kids) game development with it, and yeah, this initial learning hurdle is uncomfortably high.

    Because of that, I've been working on a new game dev environment called Mini Micro — you can read more about that here. And because it's brand new and I want to spread the word, I'm currently looking for people to mentor in game development with that (for free of course). PM me if you're interested!