Tarcza
Lekcja 15 z 20 w kursie Gra w Unity: Podniebne tory w Coddy.
Drugim ulepszeniem jest tarcza: wytrzymuje jedno trafienie. Biegacz świeci na niebiesko, gdy ją ma.
if (shield)
{
shield = false;
Destroy(other.gameObject);
return;
}
game.GameOver();Kolor biegacza to kolor jego materiału, GetComponent<Renderer>().material.color. Zachowaj kolor początkowy i co klatkę wybieraj niebieski albo ten kolor.
Wyzwanie
Łatwy- W klasie
Runnerdodajpublic bool shield;,private Renderer body;iprivate Color color;; w metodzie Start ustawbody = GetComponent<Renderer>();orazcolor = body.material.color;. - Gdy przeszkoda zostanie trafiona przy aktywnej tarczy, zużyj ją i zniszcz przeszkodę, jak wyżej.
- Tag
Shieldustawiashield = truei niszczy podniesiony przedmiot. - Na końcu metody Update:
body.material.color = shield ? new Color(0.3f, 0.9f, 1f) : color;. - W klasie
RoaddodajshieldPrefab; po magnesie umieść tarczę w ten sam sposób w blokuelse if (roll < 0.11f).
Spróbuj swoich sił
using UnityEngine;
public class Runner : MonoBehaviour
{
public float laneWidth = 2.5f;
public float laneSpeed = 16f;
public float jumpHeight = 1.6f;
public float gravity = -40f;
public float slideTime = 0.7f;
public float magnetTime = 8f;
public int lane;
public float magnet;
private CharacterController controller;
private GameManager game;
private float fallSpeed;
private float slide;
void Start()
{
controller = GetComponent<CharacterController>();
game = FindFirstObjectByType<GameManager>();
}
void Update()
{
if (!game.over)
{
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) lane = Mathf.Max(-1, lane - 1);
if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) lane = Mathf.Min(1, lane + 1);
bool jump = Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space);
if (jump && controller.isGrounded)
{
fallSpeed = Mathf.Sqrt(jumpHeight * -2f * gravity);
StopSlide();
}
if ((Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) && controller.isGrounded)
{
slide = slideTime;
Resize(0.5f);
}
}
if (slide > 0f)
{
slide -= Time.deltaTime;
if (slide <= 0f) StopSlide();
}
if (magnet > 0f) magnet -= Time.deltaTime;
if (controller.isGrounded && fallSpeed < 0f) fallSpeed = -2f;
fallSpeed += gravity * Time.deltaTime;
float targetX = lane * laneWidth;
float x = Mathf.MoveTowards(transform.position.x, targetX, laneSpeed * Time.deltaTime);
Vector3 move = new Vector3(x - transform.position.x, fallSpeed * Time.deltaTime, 0f);
controller.Move(move);
}
void StopSlide()
{
if (transform.localScale.y < 1f) Resize(1f);
slide = 0f;
}
void Resize(float height)
{
controller.enabled = false;
transform.position += Vector3.up * (height - transform.localScale.y);
transform.localScale = new Vector3(1f, height, 1f);
controller.enabled = true;
}
void OnTriggerEnter(Collider other)
{
if (game.over) return;
if (other.CompareTag("Obstacle"))
{
game.GameOver();
}
else if (other.CompareTag("Coin"))
{
game.AddCoin();
Destroy(other.gameObject);
}
else if (other.CompareTag("Magnet"))
{
magnet = magnetTime;
Destroy(other.gameObject);
}
}
}
Wszystkie lekcje w sekcji Gra w Unity: Podniebne tory
Poćwicz samodzielnie: Edytor online Unity