Drag and drop cupcakes
Here we show the creation of animations from sprites and the use of particle effects and sound effects.
Also shown is a way to drag and drop game objects in Unity.
Sample pictures
C# code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | using System.Collections; using UnityEngine; public class EatCupcake : MonoBehaviour { public GameObject eatEffect; public Animator eatAnim; private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "Cupcake") { StartCoroutine(StartEat(collision)); } } IEnumerator StartEat(Collider2D collision) { eatAnim.SetTrigger("eat"); collision.gameObject.GetComponent<Rigidbody2D>().gravityScale = 0; collision.gameObject.GetComponent<Rigidbody2D>().velocity = Vector2.zero; yield return new WaitForSeconds(.6f); eatEffect.SetActive(true); yield return new WaitForSeconds(.1f); Destroy(collision.gameObject); yield return new WaitForSeconds(1f); eatEffect.SetActive(false); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | using UnityEngine; public class Drog_Drop_mouse : MonoBehaviour { // Update is called once per frame void Update() { Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (Input.GetKey(KeyCode.Mouse0) && (GetComponent<Collider2D>() == Physics2D.OverlapPoint(mousePos))) { transform.position = new Vector2(mousePos.x, mousePos.y); } } } |