C#

--Originally published at Sierra's Blog


CLASS 1

//se ejecuta una vez al iniciar el gameobject

//no se sabe el orden de ejecución

void Awake(){

}

 

//se ejecuta después de todos los awake

//úsalo para inicialización

void Start(){

}

 

//se ejecuta una vez por frame

//pon aquí entradas de usuario y movimiento

void Update(){

}

 

void LateUpdate(){

}

 

//corre en un framerate fijo

void FixedUpdate(){

}


Class2 Collisions

// reference to other components
// all components have a corresponding class
private Transform t;
// atributos públicos
// primitivos y clases de unity pueden ser modificadas desde editor
public float velocidad;

//public GameObject go;
public Text text;

private Vector3 pos;

// Use this for initialization
void Start () {

// this can return null!!!! 
// whenever you do this do it the least times possible
t = GetComponent<Transform> ();

// go = GameObject.Find (“Text”);
// text = go.GetComponent<Text> ();
text.text = “HOLA, SÍ JALÓ”;
pos = transform.position;
}

// Update is called once per frame
void Update () {

//t.Translate (0.01f, 0, 0);
// first issue – speed 
// Time.deltaTime – keeps track of how much time passed since last frames

// input – 2 ways to catch input
// – directly pole from device
if(Input.GetKeyDown(KeyCode.A)){
//print (“KEY DOWN!”);
}

if (Input.GetKey (KeyCode.A)) {
//print (“JUST KEY.”);
}

if (Input.GetKeyUp (KeyCode.A)) {
//print (“KEY RELEASED!”);
}

if (Input.GetMouseButtonDown (0)) {
print (Input.mousePosition);
}

// – through axes (plural of axis)
float h = Input.GetAxis(“Horizontal”);
float v = Input.GetAxis (“Vertical”);
//print (h);

transform.Translate (h * velocidad * Time.deltaTime, v * velocidad * Time.deltaTime, 0, Space.World);
}

void OnCollisionEnter(Collision c){
print (c.contacts [0].point);
print (c.gameObject.transform.name);

}

void OnCollisionStay(Collision c){
//print (“stay”);
}

void OnCollisionExit(Collision c){
print(“exit”);
}

void OnTriggerEnter(Collider c){
print (“ENTER”);
Destroy (c.gameObject);
transform.position = pos;
}

void OnTriggerStay(Collider c){

}

void OnTriggerExit(Collider c){

}


Class 3 Physics

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

public class Brick : MonoBehaviour {

private Rigidbody rb;

// Use this for initialization
void Start () {

rb = GetComponent<Rigidbody> ();
Invoke (“Explotar”, 2);
}

// Update is called once per frame
void Update () {

}

void Explotar () {

rb.AddExplosionForce (2000, Vector3.zero, 10);
print (“si jala”);
}
}

 

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

public class Bullet : MonoBehaviour {

// retrieving a reference to a component
private Rigidbody rb;

// Use this for initialization
void Start () {

// this can always return null
rb = GetComponent<Rigidbody> ();

// -20
rb.AddForce (100 * transform.up, ForceMode.Impulse);
Destroy (gameObject, 3);
}

// Update is called once per frame
void Update () {

}
}

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

public class Cannon : MonoBehaviour {

public GameObject originalBullet;
public Transform punta;

// Use this for initialization
void Start () {
StartCoroutine (EjemploDeCorutina ());
}

// Update is called once per frame
void Update () {

float h = Input.GetAxis (“Horizontal”);

transform.Translate (0, 0, h * Time.deltaTime * 5);

if(Input.GetKeyUp(KeyCode.Space)){

// do the shooting.
// instantiate – clone an object
// we need a reference to the original
//Instantiate(originalBullet);
// rotation – Quaternion – vector de 4 valores, expresa rotacion en un espacio tridimensional
Instantiate(originalBullet, punta.position, transform.rotation);
}
}

// coroutine – NO es concurrencia (pero parece!)
// pseudohilos
IEnumerator EjemploDeCorutina(){

while (true) {
yield return new WaitForSeconds (0.1f);
print (“hola”);
Instantiate(originalBullet, punta.position, transform.rotation);
}
}
}


 

Class4 KEN SPRITES

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

public class Hadouken : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
transform.Translate (-7 * Time.deltaTime, 0, 0, Space.World);
}
}

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

public class Ken : MonoBehaviour {

public GameObject hadouken;
public Transform referencia;

private Animator a;
private float jAnterior;

// Use this for initialization
void Start () {
a = GetComponent<Animator> ();
jAnterior = 0;
}

// Update is called once per frame
void Update () {
float h = Input.
transform.Translate (h * Time.deltaTime * 10, 0, 0);
a.SetFloat (“speed”, h);

float j = Input.GetAxis (“Jump”);

if (jAnterior == 0 && j == 1) {
a.SetTrigger (“hadouken”);
}

jAnterior = j;
}

public void Hadouken() {
Instantiate (hadouken, referencia.position, hadouken.transform.rotation);
}

void OnCollisionEnter2D(Collision2D c){

print (“si jala”);
}

void OnTriggerEnter2D(Collider2D c){

print (“trigger”);
}
}


COROUTINES AND PATROLLING

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

public class Cannon : MonoBehaviour {

public GameObject projectile;

private IEnumerator corutina;
private float jAnterior;

// Use this for initialization
void Start () {
//StartCoroutine (Ejemplo1 ());
corutina = Ejemplo1();
jAnterior = 0;
}

// Update is called once per frame
void Update () {
float j = Input.GetAxisRaw (“Jump”);

if (jAnterior == 0 && j == 1) {
StartCoroutine (corutina);
}

if (jAnterior == 1 && j == 0) {
//StopCoroutine (corutina);
StopAllCoroutines();
}

jAnterior = j;
}

// Coroutines 
// pseudothreads
// – similar to concurrency
IEnumerator Ejemplo1(){

// use as many as needed 
// (little overhead)
while (true) {
// always have a wait of some sort
yield return new WaitForSeconds (0.2f);
print (“hola”);
Instantiate (
projectile,
transform.position,
projectile.transform.rotation
);
}
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour {

public Node[] path;
public float threshold;

private int currentNode;

// Use this for initialization
void Start () {
currentNode = 0;
StartCoroutine (HacerCambio ());
}

// Update is called once per frame
void Update () {

// 2 cosas en update
// 1. input
// 2. movimiento

// rota el objeto de tal manera que el frente de este 
// objeto ve al objetivo
transform.LookAt(path[currentNode].transform);
transform.Translate (
transform.forward *
Time.deltaTime *
5,
Space.World
);
}

IEnumerator HacerCambio(){

while (true) {
yield return new WaitForSeconds (0.1f);

float distancia = Vector3.Distance (
transform.position,
path[currentNode].transform.position
);

if (distancia < threshold) {
currentNode++;
currentNode %= path.Length;
}

}
}
}

 

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

public class Projectile : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
transform.Translate (
transform.forward * Time.deltaTime * 5
);
}
}

 

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

public class Node : MonoBehaviour {

public Node[] children;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnDrawGizmos(){

// sirve para dibujar Gizmos
// gizmos – gráficos de ayuda a desarrollador
Gizmos.color = Color.blue;
Gizmos.DrawSphere (transform.position, 1);

if (children != null && children.Length > 0) {

Gizmos.color = Color.green;
for(int i = 0; i < children.Length; i++){

Gizmos.DrawLine (
transform.position,
children[i].transform.position
);
}
}
}
}


NOTAS:

VECTOR3 https://docs.unity3d.com/ScriptReference/Vector3.html

movement:

t.Translate (h * velocidad * Time.deltaTime, v * velocidad * Time.deltaTime, 0, Space.World);

 

Collisions and triggers:

void OnCollisionEnter(Collision c){}
void OnCollisionStay(Collision c){ }
void OnCollisionExit(Collision c){ }
void OnTriggerEnter(Collider c){ }
void OnTriggerStay(Collider c){ }
void OnTriggerExit(Collider c){ }

Instatiate with rotation:

Instantiate (bullet, punta.position, punta.transform.rotation); Instantiate (bullet, punta.position, punta.transform.rotation);