Lekce 15 - Unity (C#) Android: Animace draka
V minulé lekci, Unity (C#) Android: Pauza, jsem se zabýval možností pozastavit hru.
V dnešním dílu upravíme hráče. Změníme kouli na draka přidáme mu animaci.
Video
Úprava PlayerMoveScript
Ve skriptu přibylo pouze přiřazení Animatoru a změna jeho atributu.
using UnityEngine; using System.Collections; public class PlayerMoveScript : MonoBehaviour { float flapAmount = 10; public float speed = 150; bool android; bool started = false; Animator anim; // Use this for initialization void Start() { anim = GetComponent<Animator>(); if (Application.platform == RuntimePlatform.Android) android = true; else android = false; } // Update is called once per frame void Update () { if (!started) { if (!android) { if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space)) { StartGame(); } } else { if (Input.touches.Length > 0) { StartGame(); } } return; } if(Input.GetKeyDown(KeyCode.Escape)) { Application.LoadLevel(0); } Vector3 vel = rigidbody2D.velocity; if (!android) { if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space)) { vel = Flap(vel); } } else { if (Input.touches.Length > 0) { vel = Flap(vel); } } vel.x = speed * Time.deltaTime; rigidbody2D.velocity = vel; RotateMe(vel.y); } void StartGame() { started = true; rigidbody2D.gravityScale = 2.5f; Vector3 vel = rigidbody2D.velocity; vel = Flap(vel); rigidbody2D.velocity = vel; } void RotateMe(float y) { // -20, 10 // -70, 0 // 0 -70 if(y >= 0) { transform.rotation = Quaternion.Euler(0, 0, 0); } else { float angle = Mathf.Lerp(0, -70, -y); transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, angle), 0.05f); } } Vector3 Flap(Vector3 v) { anim.SetTrigger("DoFlap"); v.y = flapAmount; return v; } }
Jak hra vypadá teď?

Problémy?
Pokud máte nějaké otázky, neváhejte se zeptat v komentářích, nebo mi napsat do zpráv.
V příští lekci, Unity (C#) Android: Obarvení draka podle zdraví, upravíme opět hráče. Drak se bude obarvovat podle svého aktuálního zdraví.