Presentation is loading. Please wait.

Presentation is loading. Please wait.

GameDevClub CODE CHEAT SHEET NOTE: ALL OF THE CODE IS CASE-SENSITIVE AND THE SYNTAX IS STRICT SO A LOT OF YOUR ERRORS WILL PROBABLY COME FROM TYPOS If.

Similar presentations


Presentation on theme: "GameDevClub CODE CHEAT SHEET NOTE: ALL OF THE CODE IS CASE-SENSITIVE AND THE SYNTAX IS STRICT SO A LOT OF YOUR ERRORS WILL PROBABLY COME FROM TYPOS If."— Presentation transcript:

1 GameDevClub CODE CHEAT SHEET NOTE: ALL OF THE CODE IS CASE-SENSITIVE AND THE SYNTAX IS STRICT SO A LOT OF YOUR ERRORS WILL PROBABLY COME FROM TYPOS If this doesn’t help, Google it lol.

2 if statements for loops while loops == //equals > //GREATER THAN, LESS THAN, GREATER THAN OR EQUAL TO, LESS THAN OR EQUAL TO = <= != //NOT equals && //AND || //OR Before we begin Just remember everything is the same as in Java!! Any line prefaced by a // is a comment //These lines //will not execute //if you put them in your code //they are simply there as notes print(“PUT STUFF HERE”); //This will print to the Unity console;

3 using UnityEngine; using System.Collections; public class MyClass : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update(){ } A new script WHEN YOU MAKE A NEW SCRIPT IT LOOKS LIKE THIS DON’T WORRY ABOUT THIS. LEAVE IT THERE MAKE SURE THE “MyClass” MATCHES YOUR FILENAME EXACTLY PUT VARIABLES HERE START FUNCTION there is a slide for these UPDATE FUNCTION WRITE MORE FUNCTIONS HERE

4 using UnityEngine; using System.Collections; public class Goomba: MonoBehaviour { private Rigidbody2D rigid; public int health = 1; public float myGravity; private bool male = false; private string name = “Bob” // Use this for initialization void Start () { rigid = GetComponent (); rigid.gravityScale = myGravity; } // Update is called once per frame void Update(){ } Adding variables Add variables to your component here You can add both basic variables and components to access. PUBLIC means other scripts can access these variables and the variable will show up in the inspector PRIVATE means other scripts can’t access these variables and it will not show up in the inspector IMPORTANT BASIC VARIABLES int // integer numbers float //numbers with decimals, Always write ‘f’ after a number with a decimal, like 1.25f bool //true or false char // single characters, use single quotes for these ‘c’ string //words, use double quotes for these Any of these with a [] after will turn it into an array

5 using UnityEngine; using System.Collections; public class Player: MonoBehaviour { string name = “Nick”; // Use this for initialization void Start () { rename(name); } // Update is called once per frame void Update(){ } public void rename(string newname){ gameObject.name = newname; } Functions (methods) You can add your own functions to scripts and call them. You can call them from other scripts too if you make the function public! using UnityEngine; using System.Collections; public class Troll: MonoBehaviour { void Start () { GameObject.FindObjectofType ().rename(“Poo”); }

6 void Start(){ // is called once, when the gameobject is initialized } void Update(){ // is called once per frame } void OnCollisionEnter2D(Collision2D other){ // is called when this object collides with another object // will only work if one of the objects has a rigidbody2D // The parameter ‘other’ gives you access to what you collided with // for example we could write print(other.gameObject.name); // to print the name of what object this collided with } void OnTriggerEnter2D(Collider2D other){ // is called when this object collides with another object with the //“is Trigger” checkbox checked // will only work if one of the objects has a rigidbody2D } Unity built-in functions You can replace ‘Enter’ with ‘Stay’ or ‘Exit’ to change when the function is called for these. You can also remove the ‘2D’. For example, the function void OnTriggerStay(Collider other){ } will be called every frame that something is in your trigger

7 SOMETHING.GetComponent (); // This will try to get a component attached to whatever SOMETHING is attached to. SOMETHING can be a component or gameObject //You usually want to set this equal to something so you can use the component you just got COMPONENTNAME nameItWhatever = SOMETHING.GetComponent (); //If you want to find the component anywhere, call: GameObject.FindObjectOfType () EXAMPLES: void Start(){ Rigidbody2D enemyRigidbody = GameObject.Find(“enemy”).GetComponent (); enemyRigidbody.velocity = new Vector2(100,0); } void OnCollisionEnter2D(Collision2D other){ other.GetComponent ().color = Color.red; } Try not to call GetComponent<>() in Update because it’s too slow of an operation to use it every frame, instead you should initialize it somewhere else and save it as a variable GetComponent

8 //The destroy function lets you destroy objects or components Destroy(ANYTHING); Destroy(this); //this will remove this script from your gameObject Destroy(gameObject); //this will destroy the gameObject in the scene Destroy(GameObject.Find(“INSERT NAME HERE”)); //this will destroy anything you want Destroy(gameObject, 1); //this will destroy the gameObject after 1 second // The instantiate function spawns a new object at a certain position and rotation Instantiate (OBJECT, POSITION, ROTATION) as GameObject; GameObject bullet = Instantiate (bullet, transform.position, Quaternion.identity) as GameObject; //Quaternion.identity gives a default rotation Instantiation and deletion

9 //In Unity we will mostly be using 2-dimensional and 3-dimensional vectors //To make a vector just write new Vector2(X,Y) //or new Vector3(X,Y,Z) Rigidbody2D.velocity requires a Vector2 All transforms require Vector3, even in a 2D game. In a 2D game just set Z to zero Vector math works! myVector = myVector.normalized //this turns your vector into the same vector with a magnitude of 1 myVector.magnitude //get the magnitude of your vector (length) myVector = myVector.normalized*X //this turns your vector into the same vector with a magnitude of X Vectors

10 Transforms: Position, rotation, scale You don’t need GetComponent to access transforms, just do: SOMETHING.transform transform.position get and set the global position transform.localPosition get and set the local position (position in relation to the parent position //ROTATION is measured in something called QUATERNIONS by default but we can use degrees too transform.rotation transform.localRotation //You can only get & set the local scale, not global transform.localScale //Set your position to X, Y, Z transform.position = new Vector3(X,Y,Z); //Set your Y position only transform.position = new Vector3(transform.position.x, Y, transform.position.z); transform.position.x = 5; //THIS WILL ERROR. YOU CANNOT DIRECTLY CHANGE ONE AXIS OF A TRANSFORM //Set your rotation to X,Y,Z transform.rotation = Quaternion.Euler(X,Y,Z); OR transform.Rotate(X,Y,Z); //Set your local Scale to X,Y,Z transform.localScale = new Vector3(X,Y,Z); Y X -X Z (depth) -Y

11 You can access children of an object through the transform transform.GetChild(0); //finds the transform of the first child of this object transform.GetChild(x); //finds the transform of the Xth child of this object transform.GetChild(0).GetChild(0); //finds the transform of the first child of the first child of this object transform.FindChild(“Name”); //finds the transform of a child named Name transform.parent; //gets the transform of this transform’s parent, if it has one If the child/parent you’re trying to get does not exist, it will return null Transforms: Parent & Child

12 void myFunction(){ //blah blah } Invoke(“myFunction”, 1); //will execute myFunction after 1 second InvokeRepeating(“myFunction”, 1,1); //will execute myFunction after 1 second, //then execute it every second after CancelInvoke(“myFunction”); //will make it stop invoking Invokes and Coroutines Coroutines are a bit tricky, but basically they execute a function over time instead of all at once. Here I made a coroutine function that grows until it is at double size. “yield return null;” makes the function wait until the next frame of the game to continue executing, in this case looping through the while loop. “yield return new WaitForSeconds(x);” waits for x seconds instead of frames void Start(){ StartCoroutine(myRoutine(2)); } IEnumerator myRoutine(float size){ while (transform.localScale.x < size){ transform.localScale = transform.localScale * 1.05f; yield return null; }

13 If you get an error in the console that says NullReferenceException, you probably tried to access something null. For example this code will find the final boss and make him 5 times bigger GameObject finalBoss = GameObject.Find(“Bowser”); finalBoss.transform.localScale = new Vector3(5,5,5); but if the game couldn’t find anything named Bowser, you’ll get a null error!!! Instead do this: GameObject finalBoss = GameObject.Find(“Bowser”); if (finalBoss != null){ finalBoss.transform.localScale = new Vector3(5,5,5); } else { print (“Couldn’t find Bowser”); } Null reference error

14 //Mathf is a class with various math functions, for example: Mathf.Sin(float) //finds the sine of an angle (RADIANS) Mathf.Pow() //does an exponential operation //Random lets you generate random values Random.value returns a float between 0 and 1 Random.Range(X, Y) returns an integer between X(inclusive) and Y(exclusive) Random.Range(0,2) would either return 0 or 1 //Time lets you mess with time Time.timeScale = x; lets you slow down and speed up time. it’s really cool! Default is 1; Time.deltaTime gets the time that has passed since the last frame. Multiply it by your transform values to make gradual transformations be based on time instead of framerate. Mathf & Random & Time

15 Application.LoadLevel(“YOUR SCENE NAME”); This loads up any scene. This is how you switch between levels Application.loadedLevelName is the current level’s name as a string So put the two together: Application.LoadLevel(Application.loadedLevelName); This will restart your scene Scenes


Download ppt "GameDevClub CODE CHEAT SHEET NOTE: ALL OF THE CODE IS CASE-SENSITIVE AND THE SYNTAX IS STRICT SO A LOT OF YOUR ERRORS WILL PROBABLY COME FROM TYPOS If."

Similar presentations


Ads by Google