Function purpose
|
Syntax
|
Spawning an object
|
Instantiate(object_name,transform.position,
Quaternion.identity);
|
Removing an object
|
Destroy(gameObject);
|
Controlling the position of an object
|
object_name.transform.Translate (x
position, y position, z position);
|
Moving an object
|
var vel = rigidbody2D.velocity;
vel.x = object_speed;
rigidbody2D.velocity = vel;
|
Referring to a particular object
|
GameObject.Find("object_name");
|
Referring to a tag
|
CompareTag ("tag_name");
|
Collision detection
|
void OnTriggerEnter2D(Collider2D);
|
Returning a random number from a specified
range
|
Random.Range(minNum, maxNum);
|
Accessing a variable from another class
|
GameObject variableName =
GameObject.Find("object_name");
Class_Name classVariableName
= variableName.GetComponent<Class_Name>();
|
Calling a function from another class
|
objectVariableName =
GameObject.Find("object_name").transform;
objectVariableName.gameObject.SendMessage
("Spawn",null,SendMessageOptions.RequireReceiver);
|
Calling a class every so seconds
|
InvokeRepeating("function_name",
startTime, repeatitionTime);
|
Adding an OnClick listener
|
Input.GetMouseButtonDown(0);
|
Reloading a level
|
Application.LoadLevel(0);
|
Spawning text
|
GUI.Label(new Rect(x position, y position, x
size, y size), "text");
|
Setting the game orientation to portrait
|
Screen.orientation =
ScreenOrientation.Portrait;
|
Playing audio
|
if (!audio.playOnAwake){
audio.Play();
}
|
Adding a swipe input
|
if(Input.touches.Length > 0){
Touch t
= Input.GetTouch(0);
if(t.phase == TouchPhase.Began){
//save
began touch 2d point
firstPressPos = new Vector2(t.position.x,t.position.y);
}
if(t.phase == TouchPhase.Moved){ //use
.Ended for slower response time
//save
ended touch 2d point
secondPressPos = new
Vector2(Input.mousePosition.x,Input.mousePosition.y);
//create vector from the two points
currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x,
secondPressPos.y - firstPressPos.y);
//normalize the 2d vector
currentSwipe.Normalize();
//swipe left
if(currentSwipe.x < 0 && currentSwipe.y > -0.5f
&& currentSwipe.y < 0.5f){
rightSwipe = false;
leftSwipe = true;
}
//swipe right
if(currentSwipe.x > 0 && currentSwipe.y > -0.5f
&& currentSwipe.y < 0.5f){
leftSwipe = false;
rightSwipe = true;
if
(!audio.playOnAwake){
audio.Play();
}
}
}
}
|
Adding a drag input
|
//when using mouse
Vector3 touchPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// get the current position of the player
GameObject
Vector3 originalPos = this.transform.position;
// replace the x coordinate with the x of
touched pos.
originalPos.x = touchPos.x;
// set the player position to the modified one
this.transform.position = originalPos;
|
Adding a highscore
|
public int score;
public int highscore;
//retrieve highscore
highscore = PlayerPrefs.GetInt("High
Score");
//set new highscore
if(score > highscore){
highscore = score;
PlayerPrefs.SetInt("High Score",
highscore);
}
|
Adding a restart button
|
if(GUI.Button(new Rect((Screen.width / 2) -
160,(Screen.height / 2) - 40, 320, 80), "Again?")) {
Application.LoadLevel(0);
}
|
6 May 2015
C# I've learned throughout dissertation
Here's a list of the C# syntax I learned throughout this dissertation:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment