0420 바인딩 오브 아이작 모작

2021. 4. 20. 15:19unity/과제

지형 지물과 ui, 바닥에 떨구는 아이템 리소스가 없어 오브젝트 직접 제작, 게임 이펙트는 전무하다.

이상하게 유니티와 비쥬얼 스튜디오가 연동이 되질 않아서 엄청 애를 먹었다....

더보기
캐릭터 이동 및 아이템 컬랙션 ui 구현

캐릭터가 아이템에 닿으면 아이템은 사라지고 ui에 숫자 업데이트

using UnityEngine;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
    public float speed;
    public Rigidbody2D rigidbody;
    public Text collectedTxt;
    public static int amount;
    // Start is called before the first frame update
    void Start()
    {
        this.rigidbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        this.rigidbody.velocity = new Vector3(horizontal * speed, vertical * speed, 0);
        this.collectedTxt.text = "Items Collected: " + amount;
    }
}
using UnityEngine;

public class Item : MonoBehaviour
{


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player") 
        {
            PlayerController.amount++;
            Debug.Log("Get heart");
            Destroy(this.gameObject);
        }
    }
}
더보기
심장 애니메이션 구현 및 이펙트 사운드 추가
using UnityEngine;

public class heart : MonoBehaviour
{
    public AudioSource sound;

    private void Start()
    {
        this.sound = GetComponent<AudioSource>();
    }
    public void Init() 
    {
        this.sound.Play();
    }
}
using UnityEngine;

public class Item : MonoBehaviour
{
    public heart heart;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            PlayerController.amount++;
            Debug.Log("Get heart");
            this.heart.Init();
            Destroy(this.gameObject);
        }
    }
}
이동과 눈물 발사 키 따로 구현
using UnityEngine;

public class BulletController : MonoBehaviour
{
    public float lifeTime;
    public AudioSource sound;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(this.DestroyDelay());
        this.sound.GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private IEnumerator DestroyDelay() 
    {
        this.playSound();
        yield return new WaitForSeconds(lifeTime);
        Destroy(this.gameObject);
    }
    private void playSound() 
    {
        this.sound.Play();
    }
}
using UnityEngine;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
    public float speed;
    public Rigidbody2D rigidbody;
    public Text collectedTxt;
    public static int amount;
    public GameObject bulletPrefab;
    public float bulletSpeed;
    private float lastFire;
    public float fireDelay;
    // Start is called before the first frame update
    void Start()
    {
        this.rigidbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        float shootHor = Input.GetAxis("ShootHorizontal");
        float shootVert = Input.GetAxis("ShootVertical");
        if ((shootHor != 0 || shootVert != 0) && Time.time > lastFire + fireDelay) 
        {
            this.Shoot(shootHor, shootVert);
            lastFire = Time.time;
        }
        this.rigidbody.velocity = new Vector3(horizontal * speed, vertical * speed, 0);
        this.collectedTxt.text = "Items Collected: " + amount;
    }
    void Shoot(float x, float y) 
    {
        GameObject bullet = Instantiate(this.bulletPrefab, transform.position, transform.rotation) as GameObject;
        bullet.AddComponent<Rigidbody2D>().gravityScale = 0;
        bullet.GetComponent<Rigidbody2D>().velocity = new Vector3(
            (x < 0) ? Mathf.Floor(x) * bulletSpeed : Mathf.Ceil(x) * bulletSpeed,
            (y < 0) ? Mathf.Floor(y) * bulletSpeed : Mathf.Ceil(y) * bulletSpeed,
            0);
    }
}

 

맵 벽과 몬스터까지 구현했는데... 몬스터 상태 왜 저러냐 ㅅㅂ
using UnityEngine;


public class EnemyController : MonoBehaviour
{
    public enum eEnemyState
    {
        WANDER, FOLLOW, DIE
    };
    GameObject player;
    public eEnemyState currState = eEnemyState.FOLLOW;
    public float range;
    public float speed;
    private bool chooseDir = false;
    private bool isDead = false;
    private Vector3 randDir;
    
    // Start is called before the first frame update
    void Start()
    {
        this.player = GameObject.FindGameObjectWithTag("Player");
    }

    // Update is called once per frame
    void Update()
    {
        switch (currState) 
        {
            case (eEnemyState.WANDER): 
                {
                    this.Wander();
                    break;
                }
            case (eEnemyState.FOLLOW):
                {
                    this.Follow();
                    break;
                }
            case (eEnemyState.DIE):
                {
                    this.Die();
                    break;
                }
        }

        if (isPlayerInRange(range) && currState != eEnemyState.DIE)
        {
            currState = eEnemyState.FOLLOW;
        }
        else if (!isPlayerInRange(range) && currState != eEnemyState.DIE) 
        {
            currState = eEnemyState.WANDER;
        }
    }

    private bool isPlayerInRange(float range)
    {
        return Vector3.Distance(transform.position, player.transform.position) <= this.range;
    }
    private void Wander() 
    {
        if (!this.chooseDir) 
        {
            StartCoroutine(ChooseDir());
        }
        transform.position += -transform.right * speed * Time.deltaTime;
        if (isPlayerInRange(range)) 
        {
            currState = eEnemyState.FOLLOW;
        }
    }
    private IEnumerator ChooseDir() 
    {
        this.chooseDir = true;
        yield return new WaitForSeconds(Random.Range(2f, 8f));
        this.randDir = new Vector3(0, 0, Random.Range(0, 360));
        Quaternion nextRotation = Quaternion.Euler(randDir);
        this.transform.rotation = Quaternion.Lerp(transform.rotation, nextRotation, Random.Range(0.5f, 2.5f));
        chooseDir = false;
    }
    private void Follow() 
    {
        transform.position = Vector2.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime);
    }

    private void Die() 
    {
        Destroy(this.gameObject, 0);
    }
}

 

더보기
몬스터 range를 설정하고 기본 스테이트를 follow로 바꿔두니 잘 작동한다.

지금 코드들 죄다 오류뜨고 난리 났는데 이거 어케 해야하냐?? 눈물만 난다.

 

 

 

m.blog.naver.com/qkrghdud0/221017441979

 

[Unity 5] Unity에서 2D Partical 제작하기 - Sprite로 파티클 만들기

안녕하세요. 호이돌입니다.요즘에 2D 게임 제작중이여서 포스팅을 많이 못하고 있네요ㅠㅠ 이번 포스팅 내...

blog.naver.com

2D 이펙트 제작하기