빈 오브젝트를 총기의 끝부분에 설정 후 발사 위치를 설정, 예전에 짠 코드라 많이 지저분함
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon_manager : MonoBehaviour
{
// 싱글톤
public static Weapon_manager instance;
// 플레이어 총기 관련
public Transform player_transform;
public GameObject player_gun_prefab;
public GameObject weapon_creation_pos_obj;
GameObject player_gun_object;
GameObject player_gun_bullet_object;
GameObject bullet_pos_object;
Vector2 player_weapon_pos;
Quaternion player_weapon_rotation_pos;
// 총알 관련
Vector2 bullet_direction;
Vector2 new_bullet_pos;
float bullet_timer;
const float bullet_reload_time = 1f;
public float bullet_power = 100f;
bool can_shot = true;
private void Awake()
{
if (instance == null) instance = this;
else Destroy(this);
}
void Start()
{
//Init_weapon_settings();
}
void Update()
{
bullet_timer += Time.deltaTime;
}
// 총기 초기화 관련
public void Init_weapon_settings()
{
bullet_pos_object = GameObject.FindGameObjectWithTag("Player_weapon_pos");
player_gun_object = Instantiate(player_gun_prefab, this.transform);
player_gun_object.transform.localPosition = weapon_creation_pos_obj.transform.localPosition;
player_gun_object.SetActive(true);
}
// 플레이어가 총기를 획득하게 됨
public void Player_get_weapon()
{
player_gun_object.transform.SetParent(player_transform);
player_gun_object.transform.localPosition = new Vector2(0.5f, -0.2f);
}
// 캐릭터 발사 관련
public void Player_used_weapon()
{
Gun_bullet gun_bullet;
// 총알 움직임
if (Singleton< Collider_manager>.instance.is_safe_zone) return;
if (bullet_timer > bullet_reload_time)
{
Audio_manager_NPC.instance.Player_shot_gun();
player_gun_bullet_object = Object_pooling_manager.instance.Dequeue_gun_bullet();
player_gun_bullet_object.transform.localPosition = bullet_pos_object.transform.position;
gun_bullet = player_gun_bullet_object.GetComponent<Gun_bullet>();
gun_bullet.bullet_direction = bullet_direction;
if (bullet_direction == Vector2.up
|| bullet_direction == Vector2.down) gun_bullet.bullet_rotation.eulerAngles = new Vector3(0f, 0f, 90f);
player_gun_bullet_object.SetActive(true);
bullet_timer = 0f;
}
}
// 캐릭터 총 관련
public void Player_weapon_change_position(KeyCode _input_key,Vector2 _player_move_pos)
{
Vector3 tmp_vector = new Vector3();
new_bullet_pos = player_gun_object.transform.localPosition;
switch (_input_key)
{
case KeyCode.W: // 윗쪽
tmp_vector = new Vector3(180f, 0f, -90f);
player_weapon_pos = new Vector2(0.5f, -0.2f);
new_bullet_pos.x += 0.1f;
new_bullet_pos.y += 0.3f;
break;
case KeyCode.A: // 왼쪽
tmp_vector = new Vector3(0f, -180f, 0f);
player_weapon_pos = new Vector2(-0.44f, -0.2f);
new_bullet_pos.x -= 0.4f;
new_bullet_pos.y += 0.1f;
break;
case KeyCode.S: // 아랫쪽
tmp_vector = new Vector3(180f, 0f, 90f);
player_weapon_pos = new Vector2(-0.44f, -0.55f);
new_bullet_pos.x -= 0.1f;
new_bullet_pos.y -= 0.3f;
break;
case KeyCode.D: // 오른쪽
tmp_vector = new Vector3(0f, 0f, 0f);
player_weapon_pos = new Vector2(0.5f, -0.2f);
new_bullet_pos.x += 0.4f;
new_bullet_pos.y += 0.1f;
break;
default: return;
}
// 총알 관련
bullet_direction = _player_move_pos;
bullet_pos_object.transform.localPosition = new_bullet_pos;
// 총기 관련
player_weapon_rotation_pos.eulerAngles = tmp_vector;
player_gun_object.transform.localPosition = player_weapon_pos;
player_gun_object.transform.rotation = player_weapon_rotation_pos;
}
}
'게임엔진 > 개인 플젝' 카테고리의 다른 글
[Outdated] 농장 시뮬레이터 개인작 (0) | 2024.04.30 |
---|---|
Flow Free 3 - UI (0) | 2024.03.27 |
Flow Free 2 - 슬롯(그리드)과 파이프(연결선) (0) | 2024.03.27 |
Flow Free 1 - 그리드 (맵) 생성 (0) | 2024.03.27 |
[Unity] Flow Free 게임 카피캣 만들기 (0) | 2023.12.05 |