본문 바로가기
2D 콘텐츠 제작 개발일지 (mario)

마리오개발 4일차

by 노재두내 2023. 9. 16.

오늘 할 일

UI 데이터 남겨서 받아오는거 하기

마리오 애니메이션

깃발 타고 내려오기 땅 아래로 떨어지면 게임 오버 

게임 오버 UI?

벽돌깨면 아이템 나오기 

껐다 켜도 남아있기 https://yoonstone-games.tistory.com/43


1. UI 씬 전환해도 남아있기

 

일단 정보 저장 없이 동적으로 UI를 생성하기

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Map2main : MonoBehaviour
{
    [SerializeField]
    private GameObject uiPrefab;
    private Transform parent;
    // Start is called before the first frame update
    void Start()
    {
        GameObject uiGo = Instantiate(this.uiPrefab);
        uiGo.transform.SetParent(parent, false);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

uiGo.transform.SetParent(parent, false); 이렇게 쓰면 그냥 드래그앤 드롭한 효과랑 같다고 한다.

동적으로 잘 생성됨


데이터 저장하고 불러오기 

오랫동안 여러 시도를 해봤는데 이런저런 문제점이 너무 많음 .. 나중에 다시 도전

 

 


2.애니메이션 구현 

using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MarioController : MonoBehaviour
{
    private Rigidbody2D rBody2D;
    private float jumpForce = 500f;
    private float moveForce = 60f;
    private Animator anim;
    private bool isJump=false;
    private bool isLongJump = false;
    [SerializeField]
    private UIDirector uiDirector;

    // Start is called before the first frame update
    void Start()
    {
        this.rBody2D = this.GetComponent<Rigidbody2D>();
        this.anim = this.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        //jump
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // 이중점프 막기
            if (this.rBody2D.velocity.y == 0)
            {
                this.rBody2D.AddForce(Vector2.up * this.jumpForce);
                if (this.isJump == false)
                {
                    this.isJump = true;
                }
            }
        }
        if (this.isJump)
        {
            if (this.rBody2D.velocity.y > 0)
            {
                // Jumping
                this.anim.SetBool("isJump", true);
            }
            else if (this.rBody2D.velocity.y < 0)
            {
                // Falling
                this.anim.SetBool("isJump", false);
            }
        }
        //if (Input.GetButtonUp("Horizontal"))
        //{
        //    this.rBody2D.velocity = new Vector2(this.rBody2D.velocity.normalized.x * 0.8f, this.rBody2D.velocity.y);
        //}
        //if (this.isJump)// space를 눌렀는데 
        //{
        //if (this.rBody2D.velocity.y > 0)// 점프상태
        //{
        // this.anim.SetInteger("State", 2);
        //}
        //}

        int dirX = 0;
    

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            dirX = -1;
        }
            
        if (Input.GetKey(KeyCode.RightArrow))
        {
            dirX = 1;
        }
        float speedX = Mathf.Abs(this.rBody2D.velocity.x);//절대값(속도는 음수 나오면 안됨), 가속도

        if (speedX < 5f)//가속도 제한 
        {
            this.rBody2D.AddForce(new Vector2(dirX, 0) * this.moveForce);
        }

        //움직이는 방향으로 쳐다보기 
        if (dirX != 0)//달리고 있다면?
        {
            //if (!this.isJump)//좌우를 쳐다보는데 점프 상태가 아니라면 => 걷는중
            //{
               this.anim.SetInteger("State", 1);
            //}
            this.transform.localScale = new Vector3(dirX * 0.5f, 0.5f, 1);
        }
        else//좌우를 쳐다보지도 않고 점프 상태도 아니라면 =>idle
        {
            //if (!this.isJump)
              this.anim.SetInteger("State", 0);
        }
        this.anim.speed = speedX / 2.0f;

        //길게 누르면 높은 점프 짧게 누르면 낮은 점프
        if (Input.GetKey(KeyCode.Space))
        {
            isLongJump = true;
        }
        else if (Input.GetKeyUp(KeyCode.Space))
        {
            isLongJump = false;
        }
    }
    private void FixedUpdate()
    {
        if (isLongJump && this.rBody2D.velocity.y > 0)
        {
            this.rBody2D.gravityScale = 1.3f;
        }
        else
        {
            this.rBody2D.gravityScale = 2.5f;
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "coin")
        {
            Destroy(other.gameObject);
            this.uiDirector.IncreaseScore(50);
            this.uiDirector.IncreaseCoin();
        }
        this.uiDirector.UpdateUI();

    }
    private void OnCollisionStay2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "portal" && Input.GetKey(KeyCode.DownArrow))
        {
            this.uiDirector.SaveData();
            SceneManager.LoadScene("Map2Scene");
 
        }
        else if (collision.gameObject.tag == "portal2" && Input.GetKey(KeyCode.RightArrow))
        {
            SceneManager.LoadScene("Map3Scene");

        }
    }
}


3. 깃발타고 내려오기 

생각해보기 ,, 깃발(tag)를 만나면 깃발 타는 애니메이션(자세)하기 mariojump애니메이션과 이어지기 ?

정확히 깃발 위치 .. 음 위치로 설정? 뛰는 위치에 따라서 다르게 하고싶은데