Unity/복습

Unity 21.04.08.복습

HappyFrog 2021. 4. 8. 23:58

버튼과 애니메이션 연결

ㄴ달리기

ㄴ공격

ㄴ정지상태

카메라 캐릭터에게 고정

UI카메라 생성 및 UI화면에 바인딩

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

public class MainCamera : MonoBehaviour
{
    GameObject hero;
    // Start is called before the first frame update
    void Start()
    {
        this.hero = GameObject.Find("Hero");
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.position = new Vector3(this.hero.transform.position.x + 2, this.hero.transform.position.y+2, this.hero.transform.position.z+3);

    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIGame : MonoBehaviour
{
    public Button btnAttack;
    public Button btnMove;
    public Button btnIdle;

    // Start is called before the first frame update
    
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InGame : MonoBehaviour
{
    public UIGame uiGame;
    public Hero hero;
    // Start is called before the first frame update
    void Start()
    {
        this.uiGame.btnAttack.onClick.AddListener(() =>
        {
            this.hero.Attack();
        });

        this.uiGame.btnMove.onClick.AddListener(() =>
        {
            this.hero.Move();
        });

        this.uiGame.btnIdle.onClick.AddListener(() =>
        {
            this.hero.Idle();
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hero : MonoBehaviour
{
    Animation anime;
    bool isMove;
    float moveSpeed = 1.0f;

    public void Attack()
    {
        this.anime.Play("attack_sword_01");
    }

    public void Move()
    {
        this.isMove = true;
        this.anime.Play("run@loop");
    }
    
    public void Idle()
    {
        this.isMove = false;
        this.anime.Play("idle@loop");
    }
    void Start()
    {
        this.anime = GetComponent<Animation>();
        this.anime.Play("idle@loop");
        this.isMove = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (this.isMove == true)
        {
            this.transform.Translate(0, 0, moveSpeed * Time.deltaTime);
        }
    }
}

바닥에 타일이 없어서 보이진 않지만 캐릭터가 z축으로 초당 1씩 이동하고있다.

============================================================================

1. 아직 언팩을 왜 하는지 이해가 안되며 오늘 처음에 empty오브젝트에 hero프리팹을 넣어서 하다가

2. Init을 잘못했는지 오류가 너무 많이나서 일단 프리팹 없이 하는 것을 복습했다.