본문 바로가기
유니티강좌

터치시 탱크로 날아가는 유도탄(유도미사일) 구현 | 레이어간 충돌 예외 처리, 탱크 좌우반전이동, 폭발음발생, 미사일 파괴 Destroy, 애니메이션 프리팹 Instantiate

by Ncube 2020. 12. 29.

 

 

대전차 유도미사일 (Antitank Guided missile / Cruise Missile)

탱크 좌우 반전하면서 왕복 이동

미사일이 탱크와 충돌했을 때
- 폭발음 발생(OnTriggerEnter2D - PlayOneShot)
- 미사일 파괴(OnTriggerEnter2D - Destroy)
- 폭발 애니메이션 프리팹 생성 (Animation - Instantiate)

미사일끼리 충돌할 때 폭발하는 걸 방지하기 위해
Edit - Project Setting - Physics 2D
레이어간의 충돌 설정(충돌 예외 처리)

유니티에서의 마우스 클릭시 미사일 스폰
안드로이드 폰 화면 터치시 미사일 스폰

[유니티 2D게임 개발(Unity & C#) 튜토리얼]

 

 

 

#유도탄미사일구현  #레이어간충돌예외처리  #폭발애니메이션프리팹생성

 

 

[TankControl.cs]

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

public class TankControl : MonoBehaviour
{
    Rigidbody2D rb;
    Vector3 localScale;
    [SerializeField]float speed = 5, dist = 7;


    bool dirRight = true;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        localScale = transform.localScale;
    }

    void FixedUpdate()
    {
        if (transform.position.x > dist)
            dirRight = false;
        else if (transform.position.x < -dist)
            dirRight = true;

        if (dirRight)
            GoRight();
        else
            GoLeft();
    }

    void GoRight()
    {
        localScale.x = -1;
        transform.transform.localScale = localScale;
        rb.velocity = new Vector2(speed, 0);
    }

    void GoLeft()
    {
        localScale.x = 1;
        transform.transform.localScale = localScale;
        rb.velocity = new Vector2(-speed, 0);
    }
}

 

 

[MissileControl.cs]

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

public class MissileControl : MonoBehaviour
{
    GameObject target;
    [SerializeField] GameObject explosion;
    [SerializeField] float speed = 2f, rotSpeed = 2f;

    Quaternion rotTarget;
    Vector3 dir;
    Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        target = GameObject.Find("Tank");
    }

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

    void GuidedMissile()
    {
        dir = (target.transform.position - transform.position).normalized;
        float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
        rotTarget = Quaternion.AngleAxis(angle, Vector3.forward);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotTarget, Time.deltaTime * rotSpeed);
        rb.velocity = new Vector2(dir.x * speed, dir.y * speed);
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        MissileSpawner.SoundPlay();
        Instantiate(explosion, transform.position, Quaternion.identity);
        Destroy(this.gameObject);
    }
}

 

 

[ExplosionControl.cs]

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

public class ExplosionControl : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Destroy(gameObject, 0.5f);
    }
}

 

 

[MissileSpawner.cs]

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

public class MissileSpawner : MonoBehaviour
{
    [SerializeField] GameObject missile;

    static AudioSource audioSource; 
    public static AudioClip audioClip;

    void Start()
    {
        audioSource = GetComponent<AudioSource>(); 
        audioClip = Resources.Load<AudioClip>("Explosion01");
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Instantiate(missile, mousePos, Quaternion.Euler(0, 0, 0));
        }
    }

    public static void SoundPlay()
    {
        audioSource.PlayOneShot(audioClip);
    }
}

 

댓글