DEV Community

gagecantrelle
gagecantrelle

Posted on

Unity (Game Engine)

When you hear the word Unity you would think. A state of being united or joined as a whole. But it has another meaning in the world of programmers, game developer's, and gamers. Unity
is a Game engine used to make video Games from 3D, 2D, Mobile, and more. Unity use C Sharp Which it harder to Learn then C++ and JavaScript. Even though Unity is a game engine it can be used in other projects like animation or some other business related project.

So what is C sharp and how is it different from C++ and C. You may be wondering, aren't C, C++, and C sharp the same?
Here are some fun facts
. C
.created in 1972
.easy to learn
.used to make operation system
.C++
.made in 1985
.steep learning curve
.upgrade to C

.C Sharp/ C#
. made in 2000
.slower than C++ and C
.have similar syntax to java, all most like a copy of java
.learning curve is some where in the middle

Since Unity can use both JavaScript and C# you might be wounded which is better? Both have similar syntax so one would think that the might be the same. But they are different in certain areas like window debuggers. In C# you need to install a debugger program. but once install when you hit the debugger button In visual studio code or another programming software. it will automatically show a debugging screen with ease. Unfortunately in JavaScript it a bit harder. You don't need to install a program to run a debugger window but, you need to create it. By making a html file and linking it to file with your code
in it. you can open a page on the internet/google and see the debugger window. Be carefully though, if you are testing something all other test you have might disappear if a part of your is not working rights

In C# when you start a new project it will create a class object for you to write you code in. While in JavaScript the class is created like CX but it hidden. Thanks to the class being hidden and line of code or function is public. meaning that it can be accesses any where. Unfortunately in C sharp it is privet meaning it will lines of codes or other function only can only be accesses in the function it was created in.

Now let get in to how to code in Unity. When you start a new project and created something like a cube. You can hit the add component button to add a script to it, being C sharp or JavaScript. Also, you must have a Visual studio code or another programing software install. Unity will automatically open your programing software to write you code. It should look something like this.

Image description

When the script is created it will automatically create a class and two function. The class will be given the name of what you called you script. The two function start and update dose what they are called. Now let try messing around with something out side of the script. Let say you created a cube and want to change one of components and give it no gravity. in your script you would first target the Ridgebody component and give it a name to reference it, let call it rb. After that go back into unity

Image description

and when you look at you script component you will notice the reference you created. Drag and drop your Rigibody component in side your rb key to target that specific Rigibody component.
Then you just put that rb inside the start function and call it useGraveity key and change it value to false. When you start you program the Rigibody component Usegravity default value
of true will be set to false.

So, now that we have a cube that has no gravity let make move. In our script file in the update function let target rb key and say AddForce(0,0,200). AddForce() if a function that apply a force to the cube and since we can't just call it, we will need to tell it how much force and in what Direction. The argument's it needs are X, Y and, Z. X will represent left to rigth, Y represent up, and down. Z represent going forward and backward. The numbers given to all three argument's will be how much force you give to the cube. Now that we got a flying cube that constantly moving forwards let give it some input controls. First let create two if statement inside the update function and give them Input.GetKey(""). Input. represent the given input and .GetKey("") represent the a key on your keyboard that you target. Let say if we target the d key it would look this .GetKey("d"). Now when ever we press the d key what ever is in the if statement will run. Since the cube we created only move in one direction, we can say if the a or b key is press. we want the cube to move left or right.

Image description

Your probably wondering How do I write AddForced() or Input.GteKey("") in either C# or JavaScrip. Since each coding langue has different syntax, ways to declare a variable, and ways to call a functions. C sharp is like a copy of Java so, if you learn Java or JavaScript and know the difference syntax used in C++. It should be hard to figure out how code in c sharp or JavaScript.

All sites links I used to gather recourses for my Blog are down blow. The images used in the blog are screen shots of YouTube video in the links down blow. this was created for a assignment.

https://www.youtube.com/watchv=j48LtUkZRjU&list=PLPV2KyIb3jR5QFsefuO2RlAgWEz6EvVi6
https://www.youtube.com/watch?v=sNMtjs_wQiE
https://www.youtube.com/watch?v=5CzNTXhxxsk part 2
https://www.youtube.com/watch?v=UeibVFhFhoc part 1

Top comments (0)