Aquí vamos aprender hacer un juego muy sencillo
El juego trataría unicamente de llevar la llave a su puerta y al llevar la llave a su puerta, una animación de un perro corriendo llegaría hasta la puerta el completar las 5 puertas habríamos terminado el nivel.
Aquí vemos como nuestra puerta ROSA no tiene ningún script ni siquiera un Box Collider, tiene un Tag con el nombre Rosa. Ahoraa vamos a ir con la llave
Aquí vemos como la llave tiene un Box Collider 2D y tiene su Tag «rosa» el mismo que tiene la llave. Su animación cuando la llave colisiones con la puerta.
Ahora os voy a poner el Script de la llave
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ejemplo : MonoBehaviour { public GameObject animacionPerro; public string Tag; public float closeVDidst = 0.05f; private Vector3 positionsss; private Vector3 offeset; float dist; GameObject GO; void Start() { GO = GameObject.FindGameObjectWithTag(Tag); } void OnMouseDown() { positionsss = Camera.main.WorldToScreenPoint(transform.position); offeset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, positionsss.z)); } void OnMouseDrag() { Vector3 curseScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, positionsss.z); Vector3 curPosition = Camera.main.ScreenToWorldPoint(curseScreenPoint) + offeset; transform.position = curPosition; Vector3 partnerPos = Camera.main.WorldToViewportPoint(GO.transform.position); Vector3 myPos = Camera.main.WorldToViewportPoint(transform.position); dist = Vector2.Distance(partnerPos, myPos); } void OnMouseUp() { Cursor.visible = true; if (dist < closeVDidst) { transform.SetParent(GO.transform); animacionPerro.gameObject.SetActive(true); Conexiones.Instance.puntosdeljuego(1); } } }
Ahora voy a poner el Script de mi animación para que se mueva de un lado a otro.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Movimientdog : MonoBehaviour { public Transform pointToGo; public float speed; public bool colliding = false; private void Update() { if (!colliding) { //Move float step = speed * Time.deltaTime; transform.position = Vector2.MoveTowards(transform.position, pointToGo.position, step); } } //Callback when enter the trigger private void OnTriggerEnter2D(Collider2D collision) { colliding = true; } }
Pagina del video: https://www.youtube.com/watch?v=ha6Oej6fjwo&t=330s
I was recommended this web site by means of my cousin. I am no
longer certain whether this put up is written by means of him
as nobody else understand such distinctive approximately my
problem. You’re wonderful! Thank you!
I like it whenever people get together and share opinions. Great blog,
keep it up!
Woah! I’m really digging the template/theme
of this website. It’s simple, yet effective. A lot of times it’s very difficult
to get that «perfect balance» between user friendliness and visual appearance.
I must say you have done a amazing job with this.
Additionally, the blog loads very quick for me on Opera.
Superb Blog!