0% found this document useful (0 votes)
37 views45 pages

Guía Lab 13

The document describes the steps taken to create a 2D game in Unity where the player drags and drops garbage objects into a garbage bin. This includes creating animations, scripts to control character movement and dragging, triggers to change movement direction, prefabs, and a UI to track garbage collection. The game was tested and improved through adding additional garbage objects, spawns, and tracking the number collected.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views45 pages

Guía Lab 13

The document describes the steps taken to create a 2D game in Unity where the player drags and drops garbage objects into a garbage bin. This includes creating animations, scripts to control character movement and dragging, triggers to change movement direction, prefabs, and a UI to track garbage collection. The game was tested and improved through adding additional garbage objects, spawns, and tracking the number collected.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

LABORATORIO 13

MG DIEGO ALONSO IQUIRA BECERRA


Crear una nueva escena
Crear Tilemap en la escena
Diseñar el nivel
Crear Carpeta Animation
Crear Animaciones Personajes
Creamos una nueva carpeta en script
Creamos un script Character
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Character : MonoBehaviour


{
public float Velocity;
public Vector2 Direccion;
public Rigidbody2D Rb2D;
// Start is called before the first frame update
void Start()
{
Rb2D = this.GetComponent<Rigidbody2D>();
Rb2D.velocity = Direccion * Velocity;
}
// Update is called once per frame
void Update()
{
}
}
Agregamos Rigidbody y Collider
Agregar Character Script y cambiar
valores
Crear script DragDropCharacter
using System.Collections; {
using System.Collections.Generic; canMove = true;
using UnityEngine; }
else
public class DragDropCharacter : MonoBehaviour {
{ canMove = false;
bool canMove; }
bool dragging; if (canMove)
Collider2D collider; {
void Start() dragging = true;
{ }
collider = GetComponent<Collider2D>(); }
canMove = false; if (dragging)
dragging = false; {
} this.transform.position = mousePos;
// Update is called once per frame }
void Update() if (Input.GetMouseButtonUp(0))
{ {
Vector2 mousePos = canMove = false;
Camera.main.ScreenToWorldPoint(Input.mousePosition);
dragging = false;
}
if (Input.GetMouseButtonDown(0))
}
{
}
if (collider == Physics2D.OverlapPoint(mousePos))
Probamos la aplicación
Modificamos el script Character
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Character : MonoBehaviour


{
public float Velocity;
public Vector2 Direccion;
public Rigidbody2D Rb2D;
// Start is called before the first frame update
void Start()
{
Rb2D = this.GetComponent<Rigidbody2D>();
Rb2D.velocity = Direccion * Velocity;
}
// Update is called once per frame
void Update()
{
}
public void ChangeDirection()
{
Rb2D.velocity = -Rb2D.velocity;
this.transform.localScale = new Vector3(-this.transform.localScale.x, this.transform.localScale.y, this.transform.localScale.z);
}
}
Creamos el Script ChangeDirection
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeDirection : MonoBehaviour


{
void OnTriggerEnter2D(Collider2D col)
{
Character Char = col.GetComponent<Character>();
if(Char!=null)
{
Char.ChangeDirection();
}
}
}
Creamos un objeto Limite y agregamos
dos hijos con collider
Probamos la aplicacion
Creamos un objeto basura y
agregamos un collider
Agregamos el script
DragDropCharacter
Probemos la aplicacion
Cambiemos la colisión a Trigger
Probemos la aplicación
Crear Objeto Basurero
Creamos Tag Basura
Cambiar Script DragDropCharacter
using System.Collections; {
using System.Collections.Generic; if (collider == Physics2D.OverlapPoint(mousePos))
using UnityEngine; {
canMove = true;
public class DragDropCharacter : MonoBehaviour }
{ else
bool canMove; {
bool dragging; canMove = false;
Collider2D collider; }
void Start() if (canMove)
{ {
collider = GetComponent<Collider2D>(); dragging = true;
canMove = false; }
dragging = false; }
} if (dragging)
public bool IsDragging() {
{ this.transform.position = mousePos;
return dragging; }
} if (Input.GetMouseButtonUp(0))
// Update is called once per frame {
void Update() canMove = false;
{ dragging = false;
Vector2 mousePos = }
Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
}
if (Input.GetMouseButtonDown(0))
Crear Script Basurero
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Basurero : MonoBehaviour


{
void OnTriggerStay2D(Collider2D col)
{
if(col.tag.Equals("Basura"))
{
DragDropCharacter Drag = col.GetComponent<DragDropCharacter>();
if (Drag != null)
{
if(!Drag.IsDragging())
{
Destroy(col.gameObject);
}
}
}
}
}
Probar Aplicación
Crear Prefab Basura
Crear Script GenerateBasura
using System.Collections; {
using System.Collections.Generic; yield return new WaitForSeconds(WaitTime);
using UnityEngine; DragDropCharacter Drag =
this.GetComponent<DragDropCharacter>();
if (Drag != null)
public class GenerateBasura : MonoBehaviour
{
{
if (!Drag.IsDragging())
public float WaitTime = 5f;
{
public GameObject BasuraPrefab;
Instantiate(BasuraPrefab,
// Start is called before the first frame update this.transform.position, Quaternion.identity);
void Start() }
{ }
StartCoroutine(EsperarBasura()); } while (true);
} }
}
// Update is called once per frame
void Update()
{
}
IEnumerator EsperarBasura()
{
do
Probar la aplicación
Crear un HUD
Agregar mas objetos basura en la
escena
Crear Script ControllerRecogerBasura
using System.Collections;
using System.Collections.Generic; // Update is called once per frame
using UnityEngine; void Update()
using UnityEngine.UI; {
ListaBasuras =
public class ControllerRecogerBasura :GameObject.FindGameObjectsWithTag("Bas
ura");
MonoBehaviour
{ if(max< ListaBasuras.Length)
// Start is called before the {
first frame update max = ListaBasuras.Length;
public Text TextoBasura; }
public GameObject[] ListaBasuras; TextoBasura.text =
private int max = 0; ListaBasuras.Length + "/" + max;
void Start() }
{ }

}
Probar la aplicacion
Modificar GameSettings
using System.Collections; NivelDesbloqueado = NivelActual;
using System.Collections.Generic; PlayerPrefs.SetFloat(NameNivelDesbloqueado,
NivelDesbloqueado);
using UnityEngine;
}
public class GameSettings : MonoBehaviour
}
{
private void Awake()
static string NameNivelDesbloqueado = "Nivel Desbloqueado";
{
static string NameNivelActual = "Nivel Actual";
NivelDesbloqueado =
public float NivelActual = 0; PlayerPrefs.GetFloat(NameNivelDesbloqueado);
public float NivelDesbloqueado = 0; NivelActual = PlayerPrefs.GetFloat(NameNivelActual);
public static GameSettings Instance { get; private set; } if (Instance != null)
public void SetValueNivel(float pos) {
{ Destroy(gameObject);
NivelDesbloqueado = pos; }
PlayerPrefs.SetFloat(NameNivelDesbloqueado, NivelDesbloqueado); else
} {
public void SetValueActual(float pos) Instance = this;
{ }
NivelActual = pos; }
PlayerPrefs.SetFloat(NameNivelActual, NivelActual); }
}
public void LevelComplete()
{
if(NivelActual> NivelDesbloqueado)
{
Agregar Objeto GameSettings
Creamos un Prefab del Character
Creamos mas personajes
Modificamos Script
ControllerRecogerBasura
using System.Collections; for (int i = 0; i < GameSettings.Instance.NivelActual; i++)
using System.Collections.Generic; {
using UnityEngine; ListaCharacter[i].SetActive(true);
using UnityEngine.SceneManagement; }
using UnityEngine.UI; }
// Update is called once per frame
public class ControllerRecogerBasura : MonoBehaviour void Update()
{ {
// Start is called before the first frame update ListaBasuras = GameObject.FindGameObjectsWithTag("Basura");
public Text TextoBasura; if(max< ListaBasuras.Length)
public GameObject[] ListaBasuras; {
public List<GameObject> ListaCharacter = new List<GameObject>(); max = ListaBasuras.Length;
private int max = 0; }
public string ScenaMapa; TextoBasura.text = ListaBasuras.Length + "/" + max;
void Start()
{ if(ListaBasuras.Length==0)
InicializarDatos(); {
} GameSettings.Instance.LevelComplete();
public void InicializarDatos() SceneManager.LoadScene(ScenaMapa);
{ }
for (int i = 0; i < ListaCharacter.Count; i++) }
{ }
ListaCharacter[i].SetActive(false);
}
Asignamos valores
Cambiamos Script ControllerMapScene
 using System.Collections;  void Update()
 using System.Collections.Generic;  {
 using UnityEngine;  if (Input.GetMouseButtonDown(0))
 using UnityEngine.SceneManagement;  {
 Vector3 mousePos =
Camera.main.ScreenToWorldPoint(Input.mousePosition);
 public class ControllerMapScene : MonoBehaviour
 Vector2 mousePos2D = new Vector2(mousePos.x,
 { mousePos.y);
 // Start is called before the first frame update  RaycastHit2D hit = Physics2D.Raycast(mousePos2D,
Vector2.zero);
 public List<ZoneScript> ListaZonas = new List<ZoneScript>();
 if (hit.collider != null)
 public string Nivel1;
 {
 void Start()
 Debug.Log(hit.collider.gameObject.name);
 {
 ZoneScript tempzone =
 UpdateZones(); hit.collider.gameObject.GetComponent<ZoneScript>();
 }  if (tempzone != null)
 public void UpdateZones()  {
 {  if (tempzone.pos ==
GameSettings.Instance.NivelDesbloqueado + 1)
 for (int i = 0; i < ListaZonas.Count; i++)
 {
 {

 ListaZonas[i].bloqueado = true; GameSettings.Instance.SetValueActual(tempzone.pos);
 }  SceneManager.LoadScene(Nivel1);
 for (int i = 0; i <
GameSettings.Instance.NivelDesbloqueado; i++)
 }
 {
 }
 ListaZonas[i].bloqueado = false;
 }
 }
 }
 }
 }
 // Update is called once per frame
 }
Agregamos a la lista de escenas
Probar la aplicación
Cambiar script Character
using System.Collections; void Update()
using System.Collections.Generic; {
using UnityEngine; }
public void SetVelocitiy()
public class Character : MonoBehaviour {
{ Rb2D.velocity = Direccion *
Velocity;
public float Velocity;
}
public Vector2 Direccion;
public void ChangeDirection()
public Rigidbody2D Rb2D;
{
// Start is called before the first
frame update Rb2D.velocity = -Rb2D.velocity;
void Start() this.transform.localScale = new
Vector3(-this.transform.localScale.x,
{ this.transform.localScale.y,
Rb2D = this.transform.localScale.z);
this.GetComponent<Rigidbody2D>(); }
Rb2D.velocity = Direccion * }
Velocity;
}
// Update is called once per frame
Cambiar ControllerRecogerBasura
using System.Collections; {
using System.Collections.Generic; ListaCharacter[i].SetActive(true);
using UnityEngine; Character Char =
ListaCharacter[i].GetComponent<Character>();
using UnityEngine.SceneManagement;
Char.SetVelocitiy();
using UnityEngine.UI;
}
}
public class ControllerRecogerBasura : MonoBehaviour
// Update is called once per frame
{
void Update()
// Start is called before the first frame update
{
public Text TextoBasura;
ListaBasuras = GameObject.FindGameObjectsWithTag("Basura");
public GameObject[] ListaBasuras;
if(max< ListaBasuras.Length)
public List<GameObject> ListaCharacter = new List<GameObject>();
{
private int max = 0;
max = ListaBasuras.Length;
public string ScenaMapa;
}
void Start()
TextoBasura.text = ListaBasuras.Length + "/" + max;
{
InicializarDatos();
if(ListaBasuras.Length==0)
}
{
public void InicializarDatos()
GameSettings.Instance.LevelComplete();
{
SceneManager.LoadScene(ScenaMapa);
for (int i = 0; i < ListaCharacter.Count; i++)
}
{
}
ListaCharacter[i].SetActive(false);
}
}
for (int i = 0; i < GameSettings.Instance.NivelActual; i++)
Probar la aplicación

You might also like