Menu
CoddyTech

방패

Coddy의 Unity 게임: 스카이 레인 코스 레슨. 20개 중 15번째.

두 번째 파워업은 방패입니다. 한 번의 공격을 막아 줍니다. 방패가 있으면 러너가 파란색으로 빛납니다.

if (shield)
{
    shield = false;
    Destroy(other.gameObject);
    return;
}
game.GameOver();

러너의 색은 머티리얼의 색인 GetComponent<Renderer>().material.color입니다. 처음 색을 유지하고, 매 프레임 파란색 또는 그 색을 선택합니다.

challenge icon

챌린지

쉬움
  1. Runner에 public bool shield;, private Renderer body; 및 private Color color;를 추가하고, Start에서 body = GetComponent<Renderer>(); 및 color = body.material.color;를 설정합니다.
  2. 방패가 켜진 상태에서 장애물에 부딪히면, 위에서처럼 방패를 사용하고 장애물을 파괴합니다.
  3. Shield 태그가 있으면 shield = true로 설정하고 아이템을 파괴합니다.
  4. Update의 끝에 다음을 추가합니다: body.material.color = shield ? new Color(0.3f, 0.9f, 1f) : color;.
  5. Road에 shieldPrefab을 추가하고, 자석 다음에 else if (roll < 0.11f)로 같은 방식으로 방패를 배치합니다.

직접 해보기

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);
        }
    }
}

Unity 게임: 스카이 레인의 모든 레슨

직접 연습해 보세요: Unity 플레이그라운드