Unity/게임 엔진 응용 프로그래밍

Vertical 2D Shooting 10 : 적기 총알발사

치명적흑형 2021. 10. 15. 12:49

적기가 플레이어의 방향으로 총을 발사한다

 


 


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

public class EnemyBullet3 : MonoBehaviour
{
    //private Vector3 curPos;
    //private Vector3 playerPos;
    private Vector3 direction;

    private void Start()
    {
        Vector3 curPos = this.transform.position;
        Vector3 playerPos = GameObject.Find("Player").transform.position;

        //위치벡터 - 위치벡터 = 방향벡터
        //playerPos - curPos 플레이어 방향으로 발사
        //curPos - playerPos 플레이어 반대 방향으로 발사
        this.direction = (playerPos - curPos).normalized;   //normalized 정규화
    }
    void Update()
    {
        this.transform.Translate(this.direction * 4 * Time.deltaTime);

        //화면을 벗어나면 파괴
        if (this.transform.position.x < -9 || this.transform.position.x > 9 || this.transform.position.y < -5.5f || this.transform.position.y > 5.5f)
        {
            Destroy(this.gameObject);
        }
    }
}

 

void Update()
    {
        this.delta += Time.deltaTime;

        if (this.delta > this.span)
        {
            GameObject bulletR = Instantiate(EnemyBullet3Prefab) as GameObject;
            bulletR.transform.position = this.transform.position;
            //발사했다면 쿨타임 초기화
            this.delta = 0;
        }
    }

 


 

 

 

 

 

 

 


 

 

https://docs.unity3d.com/kr/530/ScriptReference/Quaternion.html

 

Unity - 스크립팅 API: Quaternion

쿼터니언은 작지만, 짐벌 락(gimbal lock)에 걸리지 않고, 쉽게 보간될 수 있습니다. 유니티는 내부적으로 쿼터니언을 모든 회전을 포현하기 위해 사용합니다. 쿼터니언은 복잡한 수를 기반으로 하

docs.unity3d.com