Notice
Recent Posts
Recent Comments
Link
«   2026/06   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

개발일기

유니티#3 early return, 모듈화 초입 본문

Unity

유니티#3 early return, 모듈화 초입

kimjw7815 2024. 12. 25. 02:34
//PlayerSkill.cs
using UnityEngine;

public class PlayerSkill : MonoBehaviour //Player 오브젝트 연결
{
    public GameObject skillPrefab;
    public KeyCode skillKey = KeyCode.F;
    public float coolDownTime=3f;
    public string skillTrigger="basic";
    private float currentTime;
    private float lastTime;
    private PlayerMove playerMove;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    
    void Start()
    {
        // 생략...
    }

    // Update is called once per frame
    void Update()
    {
        // 키다운 감지
        if (!Input.GetKeyDown(skillKey)) return;
        // 무결성 검사
        if (playerMove == null || skillPrefab == null) {
            Debug.LogError("PlayerMove or SkillPrefab is null!");
            return;
        }
        Animator prefabAnimator = skillPrefab.GetComponent<Animator>();
        if (prefabAnimator == null) {
            Debug.LogError("Animator not found on skill prefab!");
            return;
        }
        currentTime=Time.time;
        if (currentTime - lastTime <= coolDownTime) {
            Debug.Log("Skill is on cooldown!");
            return;
        }
        // 대충 스킬 오브젝트 생성/제거...
    }

}

무결성 검사를 진행할 때 early return 기법을 사용했다. if문 안에 if문 안에 if문 안에...를 반복할 순 없으니까.

 

스킬 키, 프리팹, 쿨타임. 스킬 트리거 등의 설정을 public 설정으로 바꿨다.

근데 프리팹 어차피 거의 다 같고, 컴포넌트도 같은데, 그냥 애니메이션 컨트롤러만 받고 스크립트에서 생성하면 되는 거 아닌지. 이 외에도 함수도 받아서 실행시킬 수 있게 해야겠다. 이제 skillController 스크립트가 따로 있는 거지. 세상에!

 

해야할 일

  • 모듈화 끝마치기
  • Enemy HP
  • UI와 설정창 만들기
  • 스킬 봉인, 해금 만들기, 아이템 만들기

 

최근 클래식 음악을 듣고 있다, 근데 힙합을 곁들인.
https://www.youtube.com/watch?v=AeP6utJ7yNo

'Unity' 카테고리의 다른 글

유니티#6 ScriptableObject  (0) 2024.12.26
유니티#5 private 변수 참조  (0) 2024.12.26
유니티#4 스크립트 정리  (2) 2024.12.26
유니티#2 prefap  (0) 2024.12.23
유니티#1 animator  (2) 2024.12.23