Reason 1 for choosing C# – vast amount of documentation on the Internet
Have a look at the following bullet list, it will help you understand the reason for choosing C#:- C# is a well known and a heavily used programming language developed by Microsoft for creating Windows application and web-based applications. If you ever need to know anything about C#, simply do a search on the Internet.
- UnityScript is just a scripting language designed specifically for Unity. It's similar to JavaScript, yet it isn't. You may be able to search for JavaScript solutions on the web, but the code may or may not work within the confines of Unity without modification, if at all.
- Why start off learning a limited scripting language, specific only to Unity, when you can use C#, a true programming language, and find information everywhere?
- Who knows, once you see how easy C# is, maybe you might decide to develop for Windows or the Web some day. You'll already have the basics of C#.
- Once you learn C#, you'll pretty much know UnityScript, too.
Reason 2 for choosing C# – flexibility to use Unity scripts and regular C# code files
- Any C# files you have in your Unity Project folder, that are not Unity scripts, are accessible without the need of attach them to GameObjects.
- The State Machine project we will create for this book makes use of C# code files that are not attached to any GameObject.
- I'm not saying you can't create a State Machine by using UnityScript. It's just so much easier with C#. Every UnityScript file has to be attached to a GameObject to work and be accessible to other scripts. C# overcomes this necessity
Reason 3 for choosing C# – coding rules are specific
- C# is known as a strictly-typed language. What does this means to you?
- As you write code, Unity will catch coding errors immediately. Learning a subject is always easier when the rules are specific, and not some fuzzy "you can if you want to" kind of logic.
- UnityScript is not a strictly-typed language. You have the potential to write code that is not valid, but Unity won't catch the errors until you press Play.
- Finding mistakes as you write the code is so much easier than having to deal with them when a user has found them when they're playing the game.
- Please be aware, it is easy to force UnityScript to be strictly-typed, but if you're going to do that, then you may as well be using C# anyway, which brings us back to Reason 1.
Declaring a variable and its type
Every variable we want to use in a script must be declared in a statement. What does that mean? Well, before Unity can use a variable; we have to tell Unity about it first. Ok then, what are we supposed to tell Unity about the variable?There are only three absolute requirements for declaring a variable and they are as follows:
- We have to specify the type of data a variable can store
- We have to provide a name for the variable
- We have to end the declaration statement with a semi-colon
The minimum requirements for defining a method
There are three minimum requirements for defining a method:- The type of information, or data, a method will return to the place where the method was called
- The name of the method should be followed by a pair of parentheses
- A pair of curly braces should be present for containing the code bloc
Norton, Terry. Learning C# by Developing Games with Unity 3D Beginner's Guide. Birmingham, GBR: Packt Publishing, 2013. ProQuest ebrary. Web. 28 February 2015.
Copyright © 2013. Packt Publishing. All rights reserved.
The above information is from a book where it teaches you the basics of C# scripting, I pasted in those sections because I couldn't really write them down in note form. The following are parts of scripting I have learnt in lesson and researched myself, I wanted to get a more detailed understanding of scripting.
- The words function and method mean the same thing in Unity
- The named variable is nothing more than a named placeholder to store some data
- Variables and methods are the building blocks used to build unity scripts
- The main concept or idea of a class is that it's a container of data, stored in variables, and methods that process that data in some fashion
- The reason a statement ends with a semi-colon is so that unity knows when the statement ends. A full stop can't be used because they are used in the Dot Syntax
- The code between an opening curly-brace and a closing curly-brace is called a code block
- Code blocks are usually part of if statements, looking statements, and methods
- Declaring member variables at the beginning of the class means they can be used everywhere in the script
- The method name always includes a pair of parenthesis on the end. They not only show that the name is a method, but they do serve an important purpose of allowing the input of some data in the method when needed
Keywords
- Start - is called when the script is first called. For example, if the script is linked to an object, this function will be called when the object is created or added to the scene
- Update - is called every frame. This is where most game behaviour code goes, except physics code
- FixedUpdate - this function is called once every physics time step. This is the place to do physics based game behaviour. Use this for things that should be frame rate independent
- LateUpdate - is called after Update has been called. This is useful is you want to execute some functionality after all the other objects in the scene have been updated
- Awake - called when the script object is being loaded but after all GameObjects have been initialised
- Methods - where the action is and where the tasks are performed. For example if the game needs to add two numbers together a hundred different times during the game. A method can be created by writing the code to add two numbers together and giving the code block a name
- Public - means the variable will be visible in the inspector panel, and accessible through other scripts
- Private - no longer visible in the inspector panel, now a private variable to store data
- int - a simple integer, a whole number
- float - a number with a decimal place
- string - characters in double quotes, such as "Watch me go now"
- bool - a boolean, either true or false
- if statement - the most common way GameObjects make decisions. It's as easy as saying "If my condition is met, then execute my code block"
- if else statement - just like regular if statements with the else option added. If the first condition is not met then it runs the second condition after else
- ! - the NOT logical operator. Makes a true condition false, or a false condition true
- && - the AND logical operator
- || - the OR logical operator
- OnTriggerExit - is called when the Collider other has stopped touching the trigger. Only sent if one of the colliders also has rigidbody attached
- Destroy - removes a gameObject, component, or asset
- Instantiate - clones the original object and returns the clone. It has three parameters to it; original, position, and rotation
- Original - an existing object that you want to make a copy of
- Position - position for the new object
- Rotation - orientation for the new object
- Transform - position, rotation, and scale of an object
- Vector3 - representation of 3D vectors and points. As a constructor it creates a new vector with x, y, z components
- Coroutine - a function that can suspend its execution (yield) until the given YieldInstruction finishes
- WaitForSeconds - suspends the Coroutine execution for the given amount of seconds
- Quaternion - used to represent rotations
- Quaternion.Euler - returns a rotation that rotates z degrees around the z axis, x degrees around the x axis, y degrees around the y axis
- Rigidbody - control of an object's position through physics simulation
- Rigidbody.velocity - the velocity vector of the rigidbody
- Rigidbody.position - the position of the rigidbody
- Rigidbody.rotation - the rotation of the rigidbody
- Rigidbody.angularVelocity - the angular velocity of the rigidbody
- GetButton - returns true while the virtual button identified is held down
- "Fire1" - left mouse button
- MathF - a collection of math functions
- Clamp - a math function. Clamps a value between a minimum float and a maximum float value
- Random - class for generating random data
- Random.insideUnitSphere - returns a random point inside a sphere with radius 1
No comments:
Post a Comment