Xu
Bài học 13 trên 21 trong khóa học Trò chơi Unity: đường đua trên trời của Coddy.
Tiền xu tạo lý do để người chơi đổi làn khi không có vật cản nào. Mỗi đoạn có một hàng gồm bốn đồng xu trên một làn, phía sau các chướng ngại vật, ở các vị trí 4, 7, 10 và 13 dọc theo đoạn, với y bằng 1.
Mỗi đồng xu tự xoay bằng script riêng:
void Update()
{
transform.Rotate(0f, 0f, spinSpeed * Time.deltaTime);
}Prefab đồng xu nằm nghiêng, nên trục z của nó hướng lên trên: xoay quanh z sẽ làm nó quay như đồng xu trên mặt bàn.
Runner có nhiệm vụ thu thập đồng xu trong cùng OnTriggerEnter: một đồng xu gọi game.AddCoin() để cộng thêm một đồng, rồi bị phá hủy.
Thử thách
DễRải xu dọc đường để nhặt.
Coin: thêmpublic float spinSpeed = 180f;và Update ở trên.Road.Fill: ở cuối hàm, đặt 4coinPrefabvào một làn ngẫu nhiên tại y 1, z4f + i * 3f.GameManager: thêmcoinText,coinsvàAddCoin(); ShowScore cũng đặtcoinText.text = "Coins: " + coins;.Runner.OnTriggerEnter: thẻCoingọigame.AddCoin()và hủy đồng xu.
Nhấn Chơi: số xu tăng lên khi bạn nhặt chúng. Sau đó nhấn Kiểm tra.
Tự làm thử
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 int lane;
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 (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();
}
}
}
Tất cả bài học trong Trò chơi Unity: đường đua trên trời
Tự luyện tập: Playground Unity