Unity/2D RPG

캐릭터(원거리 공격) byUnity

YunSeong 2021. 7. 24. 02:37
728x90
반응형

이번에는 원거리 공격을 하는 슬라임을 만들 것이다. 

 

대포 모양의 원거리 캐릭터는 앞뒤로 찌그려졌다가 다시 펴지면서 발사하기 때문에 tranform.localScale을 건드렸다. 

1
2
3
4
5
6
7
8
9
10
IEnumerator attacking( )
{
    scale_x = transform.localScale.x;
    for (f=0; f <= 0.5; f+=0.05f)
    {
        yield return new WaitForSeconds(0.01f);
        transform.localScale = new Vector3(scale_x * ((f - 0.25f) * (f - 0.25f) * 2 + 0.875f), transform.localScale.y, transform.localScale.z);
    }
    transform.localScale = new Vector3(scale_x, transform.localScale.y, transform.localScale.z);
}
cs

(f-0.2)^2 + 0.825 라는 이차함수에 따라서 캐릭터의 x축 비율이 결정된다. 

 

 

 

 

 

 

 

대포 발사 될 때에 터지는 느낌의 효과와 실제로 나라가는 대포알을 만들기 위해서 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
IEnumerator shout()
{
    yield return new WaitForSeconds(0.05f);
        
    if (this.transform.localScale.x > 0)
    {
        this.realDirection = 1;
    }
    else if(this.transform.localScale.x < 0)
    {
        this.realDirection = -1;
    }
 
    Vector3 posForEffect = new Vector3(transform.position.x + -1 * 1.5f * this.realDirection, transform.position.y + 0.2f, transform.position.z);
    GameObject effect = Instantiate(this.shoutEffect, posForEffect, transform.rotation) as GameObject;
    effect.GetComponent<shoutEffect>().motherObject = gameObject;
 
    Vector3 posForATK = new Vector3(transform.position.x + -1 * 2 * this.realDirection, transform.position.y + 0.2f, transform.position.z);
    GameObject ball = Instantiate(this.cannonBall, posForATK, transform.rotation) as GameObject;
    ball.GetComponent<cannonBall>().motherObject = gameObject;
 
}
cs

위에서 localScale.x의 부호를 이용해서 캐릭터가 어디를 보고 있는지 판단하고 (5~12)

Instantiate를 이용해서 터지는 효과와 대포알을 소환했다. (15, 19)

그리고 이 캐릭터의 방향이나 위치같은 정보를 소환된 오브젝트에서 필요하기에 각각의 component에 오브젝트를 전달해줬다. (16, 20)

 

 

 

 

 

 

밑의 코드는 대포알의 component이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public GameObject motherObject;
Rigidbody2D rigid2D;
int realDirection;
 
void Start()
{
    rigid2D = GetComponent<Rigidbody2D>();
    this.realDirection = motherObject.GetComponent<villainTypeCannon>().realDirection;
    this.rigid2D.velocity = new Vector2(this.realDirection * -1 * 150);
}
 
void Update()
{
    this.rigid2D.AddForce(-1 * transform.up);
}
 
void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag != "Villain")
    {
        Destroy(gameObject);
    }        
}
cs

8열에서 아까 받아온 오브젝트에서 필요한 바라보는 뱡향을 realDirection 변수에 받았다.

9열에서 그것을 이용해서 속도를 정했다. 

14열에서는 중력의 영향을 구현해줬다. 

17열에서 23열까지에서는 대포알에 Trigger을 설정해주고 그 Trigger에 충돌체가 들어올 때 대포알이 없어지도록 했다. 

저부분에 if 문으로 player을 판단하고 데미지를 입히는 코드를 작성하면 될 것이다. 

 

 

 

 

 

밑의 코드는 터지는 effect의 component이다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public GameObject motherObject;
private int realDirection;
IEnumerator destoryThis()
{
    yield return new WaitForSeconds(0.15f);
    Destroy(gameObject);
}
 
void Start()
{
        
    StartCoroutine("destoryThis");
    this.realDirection = motherObject.GetComponent<villainTypeCannon>().realDirection;
    this.transform.localScale = new Vector3(-0.2f * this.realDirection, 0.2f, 0.2f);
}
cs

여기서는 그저 coroutine을 이용해서 일정 시간이 지난후 없어지고 (3~7, 12)

받아온 대포 오브젝트가 바라보는 방향에 따라서 이 오브젝트가 바라보는 방향을 정해줬다. (13~14)

 

 

 

 

이렇게 구현한 결과는 이러하다.

 

728x90
반응형

'Unity > 2D RPG' 카테고리의 다른 글

캐릭터 나눠지고 합쳐지게 by Unity  (0) 2021.08.05
friction, Collider2D in Unity  (0) 2021.08.02
회복 아이템 by Unity  (0) 2021.07.18
캐릭터(슬라임) by Unity  (0) 2021.07.18
원거리 공격 by Unity  (0) 2021.07.14