Está en la página 1de 12

APUNTES PARA EXAMEN UNITY

1. MOVIMIENTO Y VELOCIDAD (script: PlayerControl)


public class PlayerControl : MonoBehaviour {
private Rigidbody rb;
public float velocidad;
void Start () {
rb = GetComponent <Rigidbody> ();
}

void Update () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, mo
veVertical);
rb.AddForce (movement * velocidad);
}
}

**No olvidar añadirle la velocidad en el inspector.

1. 2. SALTO Y VELOCIDAD2
private bool salto;
private bool isGrounded;
public float poderSalto;
private float velocidad2 =500.0f;

void FixedUpdate (){


Saltarin ();
}
void Update () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, mo
veVertical);
rb.AddForce (movement * velocidad);
salto = Input.GetKeyDown ("space");
if (Input.GetKeyDown ("left shift")) {
rb.AddForce (movement * velocidad2);
}
}
void Saltarin (){
isGrounded = Physics.Raycast (transform.position, Vector3.down,
1.0f);
if (salto && isGrounded) {
rb.AddForce (Vector3.up * poderSalto, ForceMode.Impulse;
}
} **Añadir el valor a podersalto en el inspector.

Apuntes descargados de wuolah.com


2. CÁMARA DE SEGUIMIENTO (script: CamaraControl)
public class CameraControl : MonoBehaviour {
public GameObject player;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
}

// Update is called once per frame


void LateUpdate () { // CUIDADO: AÑADIR LATE
transform.position = player.transform.position + offset;
}
}

**Insertar el player en el inspector “player”

3. CREACIÓN DE PICKUPS ROTABLES


(Creación de prefabs + TAG: PICK UP + IS TRIGGER + Rotation: X=45)
Script: rotator
void Update () {
transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);

4. TRIGGER ON: el jugador se “come” los pickups (script: PlayerControl)

} (DONDE TERMINA VOID FIXEDUPDATE

void OnTriggerEnter (Collider otro) {


if (otro.gameObject.CompareTag ("pickup")) {
otro.gameObject.SetActive (false);
}
}
}

5. EL CANVAS (Contar coleccionables)


(Insertar texturas y convertirlas en Sprite (2D and UI)
Create/UI/Text + Create/UI/Image (contador de vidas: fondo y texto). Tamaño: 320x81.
Les añadimos la textura a la imagen en SourceImage (en el inspector). Y al texto le
ponemos 00, ubicándolo sobre la textura. **CUIDADO CON EL PIVOT, seleccionar la
esquina izquierda (tanto a las texturas como a los textos)).

Te queremos, te necesitamos, únete a nosotros.


Script: PlayerControl
using UnityEngine.UI;

public Text cuentaTexto;


public static float count;

// Use this for initialization


void Start () {
rb = GetComponent <Rigidbody> ();
count = 0;
}
void OnTriggerEnter (Collider otro) {
if (otro.gameObject.CompareTag ("pickup")) {
otro.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText (){
cuentaTexto.text = count.ToString ();
}
}

**En el inspector de player, añadimos el texto coleccionables a “CuentaTexto”

6. PRÓXIMO NIVEL (crear primero la segunda escena y meterla en el Build Setting)


Create/UI/Button. Le aplicamos la textura, y modificamos el texto. Coloco.

Script: Botón
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Botón : MonoBehaviour {


public Image proximoNivel;
public Text proximoNivel2;

// Use this for initialization


void Start () {
proximoNivel.enabled = false;
proximoNivel2.enabled = false;
}

// Update is called once per frame

Te queremos, te necesitamos, únete a nosotros.


void Update () {
if (PlayerControl.count >= 1) {
proximoNivel.enabled = true;
proximoNivel2.enabled = true;
}
}
public void IrEscena (){
SceneManager.LoadScene (SceneManager.GetActiveScene ().b
uildIndex + 1);
}
}

Se lo aplicamos a Button. En el inspector, introducimos button en proximoNivel. Y el


texto, en proximoNivel2. Luego arrastro Button desde hierarchy en OnClick, en el
inspector. Function/Botón1/IrEscena().

7. PERDER VIDAS
Primero, creamos el respawn: Create Empty object. Colocamos debajo del plano, +
AddComponent/BoxCollider: ampliamos la escala X y Z, que ocupe un gran área.
Creamos y añadimos el tag: “respawn”.
Create/Empty object. Lo llamamos “jugador”. Y lo ponemos en la posición en la que
queramos que aparezca la bola al volver a empezar.
En el canvas duplicamos el texto e imagen de los coleccionables. Cambiamos sus
nombres y les ponemos la imagen de las vidas. Los colocamos.
Duplico la escena, y la nombro como GameOver. La pongo en el build.

Script: Respawn:
public GameObject jugador;

void OnTriggerEnter (Collider otro) {


if (otro.gameObject.CompareTag ("respawn")) {
this.transform.position = jugador.transform.position;
}
}
}

Aplicamos el script al player. Y en el inspector, insertamos el empty jugador en


“jugador”.

Script respawn:
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Respawn : MonoBehaviour {


public GameObject jugador;
private float vidas;
public Text misVidas;

// Use this for initialization


void Start () {
vidas = 3.0f;
misVidas.text = vidas.ToString ();
}

void OnTriggerEnter (Collider otro) {


if (otro.gameObject.CompareTag ("respawn")) {
this.transform.position = jugador.transform.position
;
vidas = vidas - 1;
misVidas.text = vidas.ToString ();

if (vidas <= 0) {
SceneManager.LoadScene ("GameOver");
}
}
}
}

**Añado text al misVidas del player.

8. COMENZAR EL JUEGO
En el GameOver, editamos el botón siguiente. Text = comenzar. Creo un script para este
botón nuevo.
using UnityEngine.SceneManagement;

public void comenzar () {


SceneManager.LoadScene ("escena 1");
}
}

**Lo aplico al botón y arrastro el botón a onclick. Function/Comenzar/Comenzar().

9. AÑADIR MÚSICA
Creo empty/Add Component/AudioSource. Inserto la música en audioclip. Activo
LOOP.
Crear script: música.
void Awake (){
DontDestroyOnLoad (this.gameObject);
}
}
**Aplicamos el script al objeto vacío que contiene la música. Creamos y añadimos el
tag: sonar. Metemos este objeto vacío en la carpeta prefab, y lo borramos de hierarchy.

Script player control:


public GameObject sonido;

// Use this for initialization


void Start () {

Te queremos, te necesitamos, únete a nosotros.


rb = GetComponent <Rigidbody> ();
count = 0;
if (!GameObject.FindWithTag ("sonar")) {
Instantiate (sonido, transform.position, transform.rotation);
}
}
En el inspector del player control en Sonido, introducimos el prefab de música.

10. CONTADOR
Script respawn: (OJO!!! SE RESUME LAS LÍNEAS DE
vidas = vidas - 1; misVidas.text = vidas.ToString (); EN EL “VIDORRAS”, Por tanto,
borramos estas líneas y les ponemos Vidorras (); como se muestra en el script:
public Text contador;
private float tiempo;
private float tiempoFinal;

// Use this for initialization


void Start () {
vidas = 3.0f;
misVidas.text = vidas.ToString ();
tiempo = 30.0f;
tiempoFinal = 0.0f;
contador.text = tiempo.ToString ();
}

// Update is called once per frame


void Update () {
tiempo = tiempo - Time.deltaTime;
contador.text = tiempo.ToString ("00");
if (tiempo <= tiempoFinal) {
tiempo = 30.0f;
vidas--;
Vidorras ();
}
}
void OnTriggerEnter (Collider otro) {
if (otro.gameObject.CompareTag ("respawn")) {
this.transform.position = jugador.transform.position;
Vidorras ();
}
}
void Vidorras () {
vidas = vidas - 1;
misVidas.text = vidas.ToString ();
if (vidas <= 0) {
SceneManager.LoadScene ("GameOver");}}}

Te queremos, te necesitamos, únete a nosotros.


SCRIPTS COMPLETOS:

SCRIPT CAMERA CONTROL:


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

public class CameraControl : MonoBehaviour {


public GameObject player;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
}

// Update is called once per frame


void LateUpdate () {
transform.position = player.transform.position + offset;
}
}
SCRIPT PLAYER CONTROL:

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

public class PlayerControl : MonoBehaviour {


private Rigidbody rb;
public float velocidad;
private bool salto;
private bool isGrounded;
public float poderSalto;
private float velocidad2 =500.0f;
public Text cuentaTexto;
public static float count;
public GameObject sonido;

// Use this for initialization


void Start () {
rb = GetComponent <Rigidbody> ();
count = 0;
if (!GameObject.FindWithTag ("sonar")) {
Instantiate (sonido, transform.position, transform.rot
ation);
}
}

Te queremos, te necesitamos, únete a nosotros.


void FixedUpdate (){
Saltarin ();

// Update is called once per frame


void Update () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, mo
veVertical);
rb.AddForce (movement * velocidad);
salto = Input.GetKeyDown ("space");
if (Input.GetKeyDown ("left shift")) {
rb.AddForce (movement * velocidad2);
}
}
void Saltarin (){
isGrounded = Physics.Raycast (transform.position, Vector
3.down, 1.0f);
if (salto && isGrounded) {
rb.AddForce (Vector3.up * poderSalto, ForceMode.Impu
lse);
}
}

void OnTriggerEnter (Collider otro) {


if (otro.gameObject.CompareTag ("pickup")) {
otro.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText (){
cuentaTexto.text = count.ToString ();
}
}
SCRIPT ROTATOR

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

public class Rotator : MonoBehaviour {

// Update is called once per frame


void Update () {
transform.Rotate (new Vector3(15,30,45) * Time.deltaTime
);

}
}

SCRIPT BOTÓN
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Botón : MonoBehaviour {


public Image proximoNivel;
public Text proximoNivel2;

// Use this for initialization


void Start () {
proximoNivel.enabled = false;
proximoNivel2.enabled = false;
}

// Update is called once per frame


void Update () {
if (PlayerControl.count >= 1) {
proximoNivel.enabled = true;
proximoNivel2.enabled = true;
}
}
public void IrEscena (){
SceneManager.LoadScene (SceneManager.GetActiveScene ().b
uildIndex + 1);
}
}

Te queremos, te necesitamos, únete a nosotros.


SCRIPT RESPAWN
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Respawn : MonoBehaviour {


public GameObject jugador;
private float vidas;
public Text misVidas;
public Text contador;
private float tiempo;
private float tiempoFinal;

// Use this for initialization


void Start () {
vidas = 3.0f;
misVidas.text = vidas.ToString ();
tiempo = 30.0f;
tiempoFinal = 0.0f;
contador.text = tiempo.ToString ();
}

// Update is called once per frame


void Update () {
tiempo = tiempo - Time.deltaTime;
contador.text = tiempo.ToString ("00");
if (tiempo <= tiempoFinal) {
tiempo = 30.0f;
vidas--;
Vidorras ();
}
}
void OnTriggerEnter (Collider otro) {
if (otro.gameObject.CompareTag ("respawn")) {
this.transform.position = jugador.transform.position
;
Vidorras ();
}
}

void Vidorras () {
vidas = vidas - 1;
misVidas.text = vidas.ToString ();

if (vidas <= 0) {
SceneManager.LoadScene ("GameOver");
}

Te queremos, te necesitamos, únete a nosotros.


}
}

SCRIPT MÚSICA
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Música : MonoBehaviour {

void Awake (){


DontDestroyOnLoad (this.gameObject);
}
}
SCRIPT COMENZAR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Comenzar : MonoBehaviour {

public void comenzar () {


SceneManager.LoadScene ("escena 1");
}
}

Te queremos, te necesitamos, únete a nosotros.

También podría gustarte