- 오늘의 할일

- 플레이어 이동

- GManager 생성

- 2D용 LookAt

 

-플레이어 이동

먼저 플레이어 캐릭터로 사용할 임시 이미지를 설정햇다

플레이어 캐릭터

컴퓨터 포맷을 위해 자료를 정리하던중 찾은 캐릭터 PNG이미지를 활용하여 맹글었다.

 

스프라이트 에디터로 슬라이스 해주었다

슬라이스 후 Player오브젝트를 만들고

플레이어 -> 무기생성위치베이스 -> 무기

GManager 이름의 오브젝트도 생성해주었따

 

플레이어에 간단한 이동만 추가주고

 

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

public class Player : MonoBehaviour
{
    MoveType.Type m_moveType = MoveType.Type.Idle;

    DirType.Type m_dirType = DirType.Type.Left;

    public float m_speed = 0.0f;

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

    void MoveController()
    {
        Vector2 _input = Vector2.zero;
        _input.x = Input.GetAxisRaw("Horizontal");
        _input.y = Input.GetAxisRaw("Vertical");

        transform.Translate(m_speed * Time.deltaTime * _input.normalized);
    }
}

 

Gmanager에도 스크립트 부착

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

public class GManager : MonoBehaviour
{
    static GManager g_instance = null;

    public static GManager Instance
    {
        get
        {
            return g_instance;
        }
    }
    private void Awake()
    {
        if (GManager.Instance == null)
        {
            g_instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }

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

    /// <summary>
    /// 2d용 LookAt
    /// </summary>
    /// <param name="argStartPos">현재위치</param>
    /// <param name="argTargetPos">처다볼위치</param>
    /// <param name="argFixRot">수정할 회전값</param>
    /// <returns>각도</returns>
    public Quaternion LookAt2D(Vector2 argStartPos, Vector2 argTargetPos, float argFixRot = 0.0f)
    {
        Vector2 _dir = argTargetPos - argStartPos;
        float _angle = Mathf.Atan2(_dir.y, _dir.x) * Mathf.Rad2Deg - argFixRot;

        return Quaternion.AngleAxis(_angle, Vector3.forward);
    }
}

Gmanager의 스크립트: 싱글톤 패턴

천상천하 유아독존

건방진 Gmanager는 단 하나만 존재할 수 있다

 

 

WeaponBase에도 스크립트 부착

public class WeaponBase : MonoBehaviour
{
    private void LateUpdate()
    {
        Vector2 _pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.rotation = GManager.Instance.LookAt2D(transform.position, _pos);
    }
}

GManager 스크립트에서 가져온 LookAt2D

 

 

***결과물***

마우스를 잘 따라가고 잘 움직여진다

 

'파쿠리프로젝트' 카테고리의 다른 글

파쿠리프로젝트(2)  (0) 2022.12.31
파쿠리 프로젝트(0)  (0) 2022.12.28

+ Recent posts