반응형

그림파일을 Texture 폴더에 끌어 옮겨 저장합니다 

 

Gun 텍스쳐타입을 아래 그림과 같이 바꿉니다 

 

 

 

 

Gun 그림파일의 속성에서 Sprite Editor를 들어가서 Pivot 점을 방아쇠 근처로 옮깁니다 

 

Gun 텍스쳐를 Hierachy 에 옮기고  Gun 오브젝트를 선택하여 Sorting Layer를 하나 만들고 order in Layer 1로 합니다 

 

bullet 텍스쳐를 Hierachy로 옮기고 SortingLayer를 Bullet을 만들고 Order in Layer 1로 합니다 

 

 

 

 

Prefabs 폴더를 만들고 드래그하여 프리팹을 만듭니다 

 

Gun 오브젝트를 Player 자식으로 옮기고 Gun 오브젝트에 GunCtrl 스크립트를 생성하여 작성합니다 

 

 

 

 

GunCtrl 스크립트를 작성합니다 

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

public class GunCtrl : MonoBehaviour
{

    void Update()
    {
        //카메라 스크린의 마우스 거리와 총과의 방향 
        Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        //마우스 거리로 부터 각도 계산
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        //축으로부터 방향과 각도의 회전값
        Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        transform.rotation = rotation;
    }
}

 

 

 

 

 

게임을 실행 시키고 아래 동영상과 같이 마우스 움직임에 따라 총이 움직이는 것을 확인합니다 

Texture2.zip
0.04MB

반응형

+ Recent posts