Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Unity and C# courses

Options
  • 27-10-2013 9:40pm
    #1
    Registered Users Posts: 3,831 ✭✭✭


    Hey folks,
    I have been digging around online to try find a course or tutorial series that will help me progress with Unity and C#.

    I keep hitting small issues with scripting that I can't seen to troubleshoot no matter how many times I search references or other tutorials.
    Most recently has been some Auto Turrets I am working on, where instantiating bullet clones is hitting a wall with reference errors and similar type issues.

    I think I need to learn more about what exactly is happening with instantiation and see various methods of using it in that case.

    I just can't find any good tutorials so far.
    They all do a simplified version or something that is no use to me and does not help me understand the issues I am seeing.

    So I am going here as a near last resort to finding some hidden secret knowledge on C# and oop. :D
    I can't afford to pay for courses, so hoping i can find a free online course amongst the many out there that would help be get a better solid grasp of the foundations.

    3D buzz is really good for example, but all their free content I have either been through or I already know for the most part in relation to this stuff.

    I am even open to doing any C# course not even game related (as I may need it anyway for databasing and networking later).

    Would appreciate help, I don't think its helpfull to post every little issue I come across.
    You know the saying, give a man a fish.... give him a rod..etc


Comments

  • Registered Users Posts: 454 ✭✭Kilgore__Trout


    Hello Torak,

    Few tutorials for you. If you're fairly new to Unity, you could look at this one. It's in javascript, but it's good for getting the basics of using the engine down.

    http://www.youtube.com/watch?v=5-X-Ebh1kYA&list=PL74F71544CE701190


    This one is for a hack and slash in C#. There's dozens of hours in it, and the guy is really good at explaining what he's doing. The game is a little complex though. Didn't finish it, but you should be able to pick up a few tricks.

    http://www.youtube.com/watch?v=YYqzz1dy3Ak&list=PLE5C2870574BF4B06

    After that, you could look at the Unity Store. Lots of free tutorials. Could also make some simple games like pong, breakout (if you haven't already).

    If you want to get a better idea of general programming, try looking up the new boston on youtube. Not sure if he has C# tutorials, but there's lots of java tutorials. Found the two languages to be nearly identical, down to method names.


  • Registered Users Posts: 3,831 ✭✭✭Torakx


    Thanks for those links.
    I had forgotten about some of those ones.
    Have spent so many hours trawling through tutorials on youtube lol

    I found that nearly all of them miss some explanations on why something is there.
    Especially with regards to referencing variables, objects etc.

    Stuff like stating a transform before the start function

    Then in start something like myTransform = Transform;

    Then using that reference in update for instantiating a clone.
    I am having various issues still with this type of stuff and think I need a course in C# to undestand exactly whats happening.
    I have used all these things before to make my 2D sidescrolling shooter in college and it worked fine, even usng arrays and lists to change weapon bonuses etc.

    Sometimes the same codes from that game just won't work again in another..
    But I will go over those links and see if anything there can help me, thanks :)


  • Registered Users Posts: 454 ✭✭Kilgore__Trout


    Takes a while to get to grips with Unity. Keep at it, and the pieces will start to fall into place.

    I'd be inclined to think of Prefabs in Unity as classes, from which you create instances (objects). Here's a simple example of a script, which controls spawning of enemies. You need to make an enemy gameobject, and assign it as a prefab.

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    public class ControlScript : MonoBehaviour
    {
    // Fields
    public GameObject enemyPrefab; 1*

    // Mono Methods
    void Update()
    {
    Instantiate(enemyPrefab, location, Quaternion.identity);2*
    }

    }

    1* making it public means you can assign the prefab to controlscript in the inspector. This is just a reference to the prefab you want to spawn a gameobject from.

    2* Instantiate takes three vars: the prefab you want to spawn, the location you want to spawn it at (Vector 3), and the starting rotation of the object (Quaternion).

    This script would call instantiate in every update, and would kill the frame rate, so you'd need a timer to control the spawning.


  • Registered Users Posts: 3,831 ✭✭✭Torakx


    Hmm that is fairly basic stuff I covered. which makes me wonder have I just been oercomplicating things. I have a habit of doing that ALL the time!
    Heres my curent turret script that has a few issues I still need to fix.
    Actually I need to know how to make it wrapped so you can click to drop it down here first.
    Anyone know?
    I tried spoiler but no joy.

    Ok I cut out the important parts of the script to give people an idea of what I'm delaing with generally.
    public class turret_script : MonoBehaviour 
    {
    	public Rigidbody bullet;
    	public float speed1 = 3f;
    	
    	
    	private Transform bullet_spawn;
    	public int rotationSpeed = 2;
    	public int move_speed = 2;
    	public Transform target;
    	public static bool contact = false;
    	bool timer_go = false;
    	public float shoot_timer = 2f;
    	public static bool can_fire = false;	
    	private Transform turret_1;
    
    void Start () 
    	{
    		bullet_spawn = transform.Find ("bullet_spawn");
    		GameObject go = GameObject.FindGameObjectWithTag("enemy");
    		target = go.transform;
    	}
    
    void Update ()
    
                      if(can_fire == true)
    			{
                                    // line under this has a null reference exception.
    				Rigidbody bulletClone = (Rigidbody) Instantiate(bullet, bullet_spawn.transform.position, transform.rotation);
    				bulletClone.velocity = transform.forward * speed1;
    				shoot_timer = 0.6f;
    				timer_go = true;
                             }
    

    Still no joy on learnign to make a drop down code wrap for the forum post.

    On another note, I might do a thread like a tutorial for people new to Unity and C#.
    Like a list of handy functions and how they interact. Would probably help me sharpen my knowledge too.

    Oh also, I am online quite a lot doing various research and dev stuff, if anyone is into Unity and/or C# I'd be happy to add them to my skype and chat now and then about coding and Unity stuff.
    I'm looking to build up my contacts in the industry and find people to collaborate with.
    If anyones interested send me a private message with their skype contact and I'l add them.


  • Registered Users Posts: 3,831 ✭✭✭Torakx


    Found the solution and so I should probably explain the answer for anyone else following along behind me who may need the same solution.
    This would have helped me a great deal.

    Instantiating a projectile in Unity with C#

    Create your gun object(or import an fbx which would be a prefab already) and drag it to the relevant folder in the Project panel to make it a prefab.
    Do the same for your projectile.

    Now put your gun in the scene and on the toolbar up top go to GameObject dropdown and create empty.
    Name it bullet_spawn.
    Put the empy GameObject at the end of your gun and in my case I have the Z axis pointing away from the gun so the bullet will go in that direction, also known as .forward in code IIRC.

    create a C# script called "gun" case sensitive to the script i'l supply.
    Highlight absolutely everthing in this new script and delete it.
    Then copy paste all my code in.
    There is a lot of comments you can delete them if you like.

    This will create a projectile named "bulletClone" every frame from when you hit play in Unity.
    So be ready to stop it fast!
    Best to put a timer on it or a fire button with the instantiate part inside an if statment or something.
    if ( something happens)
    {
    instantiate etc
    }
    using UnityEngine;
    using System.Collections;
    
    public class gun : MonoBehaviour
    {
        //Private anything only applies to methods etc in this script.Wont be able to use it from another or see it in inspector.
        private GameObject bullet_spawn;
        
        // a public reference for you to drag any prefab in the editor, to the inspector panel for the script.
        public Rigidbody bullet; 
        
        // you can make this private if you don't want it to be changed in the editor inspector panel.
        public float bullet_speed = 500f;
            
        // Use this for initialization
        void Start ()
        {
            bullet_spawn = GameObject.Find ("bullet_spawn"); //I think this loads the gameobject into memory..not 100% sure.
        }
        
        // Update is called once per frame
        void Update ()
        {
            // First two words create a new rigidbody into memory named bulletClone.
            // this new object = what you instantiate, the (rigidbody) I am guessing lets it know what type.
            // It might be why I don't need to put    as Rigidbody;   at the end of that line.
            // this code in update will create a projectile every frame the program runs. So like 30 a second?
            // it's advised you add a way to fire or a timer..or just be ready to stop playing the scene otherwise :)
            Rigidbody bulletClone = (Rigidbody) Instantiate(bullet, bullet_spawn.transform.position, bullet_spawn.transform.rotation);
            bulletClone.velocity = transform.forward * bullet_speed;
            
            
        }
    }
    

    I think thats it for a basic projectile.
    I ran it to test and works fine for me after adding it to the top section of my turret.So you should be able to add this to your gun object in Unity and drag the relevant prefabs like the bullet prefab into the script area on the inspector.

    The issue I had was instead of referencing the bullet_spawn as GameObject at the top, I had it as public Transfrom bullet_spawn;
    It had a Null reference exception, becuase it took exception to the transform when it wanted to know what object to use .transform for.
    In that instantiate code if you wanted to spawn the bullet from the transform of the object the script is attached to, you just take out bullet_spawn from the .transform.position and from .transform.rotation and add this. instead.

    like this:

    Rigidbody bulletClone = (Rigidbody) Instantiate(bullet, this.transform.position, this.transform.rotation);

    or possibly just use

    Rigidbody bulletClone = (Rigidbody) Instantiate(bullet, transform.position, transform.rotation);


    And to keep on topic..
    Keep an eye out for some decent C# courses for me :D
    Not looking for tutorials, more like a solid course that will be boring but effective most likely lol

    Have fun and share your work!
    Experiment with it.


  • Advertisement
  • Registered Users Posts: 454 ✭✭Kilgore__Trout


    Looks like you just leveled up, Torakx : )


  • Registered Users Posts: 3,831 ✭✭✭Torakx


    Looks like you just leveled up, Torakx : )
    Hahahah!
    Yeah seems so, took ages to find that silly little issue!
    I got a great tip form another coder recently.
    She said to try find other coders to take 5 mins to look at your work and see if they spot something where you have been staring all day.
    Like send it to them and ask,"if you can spot it within 5 mins grand" otherwisse leave it.
    I like that idea.No pressure and I am sure it would help a lot in the long run.


Advertisement