앱 만들기/유니티2D

유니티-8방향으로 총알을 발사하다.

나도 처음이야 2021. 7. 1.

타임 파이럿 처럼 

8방향으로 움직이면서 사방으로 총알을 쏘기위해서는

https://www.youtube.com/watch?v=AgNTahUrJCI 

 

https://soo0100.tistory.com/1553

 

유니티 - 전투기 8방향으로 이동 하기.

타임 파일럿 이라는 아케이드 게임있었다. 사실 난 접해보지 못한 오락실 게임이였는데, 당시 비행 슈팅게임의 한 획을 그은 게임이라고 한다. 보시는 것 처럼, 전투기가 8방향으로 움직인다. 유

soo0100.tistory.com

위 8방향으로 이동 포스팅 이후

8방향으로 총알을 쏘는 방법을 구현해 본다.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class OnFire : MonoBehaviour
{
    public GameObject missilePrefab;
    public int maxCount = 20;
 
    private float offSetX = 1.5f; // 비행체와 미사일 간격
    private float offSetY = -0.4f; // 비행체와 미사일 간격
    private int direction = 0// 오른쪽
    bool pushFlag = false;
    // 미사일
    
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("right")) // 오른쪽 키가 눌렸을때.
        {
            direction = 0;
            offSetX = 1.5f;
            offSetY = -0.4f;
        }
        else if (Input.GetKey("left")) //  왼쪽 키가 눌렸을때.
        {
            direction = 1;
            offSetX = -1.5f;
            offSetY = -0.4f;
        }
        else if (Input.GetKey("up")) // 오른쪽 키가 눌렸을때.
        {
            direction = 2;
            offSetX = 0f;
            offSetY = 1.5f;
        }
        else if (Input.GetKey("down")) // 오른쪽 키가 눌렸을때.
        {
            direction = 3;
            offSetX = 0f;
            offSetY = -1.5f;
        }
        if (Input.GetKey("right"&& Input.GetKey("up"))
        {
            direction = 4;
            offSetX = 1.5f;  
            offSetY = 1.5f;
        }
        if (Input.GetKey("left"&& Input.GetKey("up"))
        {
            direction = 5;
            offSetX = -1.5f;
            offSetY = 1.5f;
        }
        if (Input.GetKey("right"&& Input.GetKey("down"))
        {
            direction = 6;
            offSetX = 1.5f;
            offSetY = -1.5f;
        }
        if (Input.GetKey("left"&& Input.GetKey("down"))
        {
            direction = 7;
            offSetX = -1.5f;
            offSetY = -1.5f;
        }
 
        if (Input.GetKey(KeyCode.Space)) // 스페이스가 눌렸을때
        {
            if (pushFlag == false)
            {
                pushFlag = true;
                Vector3 area = GetComponent<SpriteRenderer>().bounds.size;
                Vector3 newPos = this.transform.position;
                newPos.x += offSetX;
                newPos.y += offSetY;
                //프리팹을 만든다.
                GameObject missile = Instantiate(missilePrefab) as GameObject;
                newPos.z = -5;
                missile.transform.position = newPos;
                Debug.Log("direction = " + direction);
                Rigidbody2D rbody = missile.GetComponent<Rigidbody2D>();
                if (direction == 0)
                    rbody.AddForce(Vector2.right*20, ForceMode2D.Impulse);
                else if (direction == 1)//왼쪽
                    rbody.AddForce(Vector2.left*20, ForceMode2D.Impulse);
                else if (direction == 2)//위
                    rbody.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
                else if (direction == 3)//아래
                    rbody.AddForce(Vector2.down * 10, ForceMode2D.Impulse);
                else if (direction == 4)//오른쪽 대각선 위(X축 10, Y축 10 만큼의 힘 주기)
                    rbody.AddForce(new Vector2(1010), ForceMode2D.Impulse);              
                else if (direction == 5)//왼쪽 대각선 위
                    rbody.AddForce(new Vector2(-1010), ForceMode2D.Impulse);
                else if (direction == 6)//오른쪽 밑
                    rbody.AddForce(new Vector2(10-10), ForceMode2D.Impulse);
                else if (direction == 7)//왼쪽 밑
                    rbody.AddForce(new Vector2(-10-10), ForceMode2D.Impulse);
            }
            else
            {
                pushFlag = false;
            }
        }
    }
}
 
cs

위 코드에서 중요한점은 바로.

 rbody.AddForce  함수이다.  

하기 라인을 해석하자면, X축으로 10의 힘을, Y축으로 10의 힘을 주어서 발사하라는 의미로 해석될 수 있다.

즉, X,Y축 10씩의 힘은 바로 대각선으로 쏜다는 의미이다.

   rbody.AddForce(new Vector2(1010), ForceMode2D.Impulse);    

 

이렇게 구현하면, 손쉽게 여러방향으로의 총알 구현이 가능하다.

감사합니다 ^^

반응형

댓글