*미사일 생성 하기
Bullet에 player bullet 0 과 player bullet 1을 끌어다 놓고 player bullet 1을 복사해서 만든다
이름을 Center와 Left, Right로 이름을 변경해준다
Center의 위치를 0으로 맞추고 크기를 2로 해준다
Left와 Right의 위치를 -0.4, 0.4 로 해주고 크기는 1로 해준다
빈 오브젝트 Player Bullet을 만들어서 포지션을 0으로 해준다
Center, Left, Right 오브젝트를 안에 넣어준다
합쳐져서 하나처럼 만들어진다
프리팹 사용하여 미사일 복사하기
프로젝트에 프리팹 파일을 만들고 Player Bullet 오브젝트를 프리팹에 넣는다
이름을 Player Bullet Prefeb 으로 변경하고
Player Bullet 오브젝트를 삭제하고
Player Bullet Prefeb를 하이에라키에 끌어온다 (복사 : Ctrl + D)
*버튼 생성
+를 누르고 UI에 Button을 눌러 오브젝트 생성한다
포지션을 0으로 하고 이름을 btnA로 바꾼다
프로젝트에서 Button A를 btn A의 Image에 Source Image에 끌어다 넣는다
아래에 Set Native Size를 눌러 사이즈를 맞게 한다
btn A 오브젝트 안에 Text를 지운다
버튼 완성
버튼을 누르면 눌린다
btn A 를 클리하고 오른 쪽 하단에 Add Component를 클릭하여 Event Trigger를 생성한다
비주얼 스튜디오에서 Start Shoot과 Stop Shoot 메서드를 만들고 코드 작성 한다
Player 오브젝트에 프리팹을 끌어다가 넣어 준다
그 다음 Player를 클릭하고 새로운 오브젝트를 생성하고 이름을 BulletPos로 한다
포지션을 0으로 하고 Player 앞에 좌표를 둔다
프리팹을 넣고 좌표를 맞춘다
그리고 코드 작성
public Transform bulletPoint; 로 뚫는다
그리고 BulletPos -> BulletPoint로 이름을 변경하고
BulletPoint에 오브젝트를 넣는다
그러면 미사일이 계속 생겨난다
*미사일 발사
그 다음 Bullet 스크립트 생성한다
그리고 코드 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed = 3f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.Translate(Vector2.up * this.speed * Time.deltaTime);
}
}
프리팹을 클릭하고 Open Prefeb 하고
Bullet을 프리팹 안에 넣어준다
그리고 코드 입력
"1"을 this.span으로 변경
public float = 0.5f; 작성
오른쪽 하단의 span을 0.3으로 설정한다
0에 가까울 수록 공격 속도가 빨라진다
Bullet 속도를 6으로 하여 속도를 올린다
미사일 발사 완성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public bool isPressLeftArrow;
public bool isPressRightArrow;
public bool isPressUpArrow;
public bool isPressDownArrow;
public int prevKey; // -1 : left, 1 : right
public float speed = 2.0f;
private Animator animator;
private bool isShoot;
private float delta; //총알 발사시 딜레이 누적 시간
public GameObject bulletPrefab;
public Transform bulletPoint;
public float span = 0.5f;
// Start is called before the first frame update
void Start()
{
this.animator = this.GetComponent<Animator>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
this.isPressLeftArrow = true;
if (this.isPressRightArrow)
{
prevKey = 1;
this.isPressRightArrow = false;
}
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
this.isPressRightArrow = true;
if (this.isPressLeftArrow)
{
prevKey = -1;
this.isPressLeftArrow = false;
}
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
this.isPressUpArrow = true;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
this.isPressDownArrow = true;
}
if (Input.GetKeyUp(KeyCode.LeftArrow))
{
this.isPressLeftArrow = false;
if (this.prevKey == -1) //내가 띤 키가 이전 키라면 초기화
{
this.prevKey = 0;
}
if (this.prevKey == 1)
{
this.isPressRightArrow = true;
this.prevKey = 0;
}
}
if (Input.GetKeyUp(KeyCode.RightArrow))
{
this.isPressRightArrow = false;
if (this.prevKey == 1) //내가 띤 키가 이전 키라면 초기화
{
this.prevKey = 0;
}
if (this.prevKey == -1)
{
this.isPressLeftArrow = true;
this.prevKey = 0;
}
}
if (Input.GetKeyUp(KeyCode.UpArrow))
{
this.isPressUpArrow = false;
}
if (Input.GetKeyUp(KeyCode.DownArrow))
{
this.isPressDownArrow = false;
}
int h = 0;
if (this.isPressLeftArrow) h = -1;
else if (this.isPressRightArrow) h = 1;
else h = 0;
//Debug.LogFormat("{0}, {1}", this.isPressLeftArrow, this.isPressRightArrow);
this.animator.SetBool("IsPressLeftArrow", this.isPressLeftArrow);
this.animator.SetBool("IsPressRightArrow", this.isPressRightArrow);
//float v = Input.GetAxis("Vertical"); //키보드 입력을 -1... 0 ... 1 까지 수로 반환 (소수점)
//float h = Input.GetAxis("Horizontal");
float v = Input.GetAxisRaw("Vertical"); //키보드 입력을 -1, 0, 1 까지 수로 반환 (정수)
//float h = Input.GetAxisRaw("Horizontal");
if (Input.GetKey(KeyCode.LeftArrow) && Input.GetKey(KeyCode.RightArrow))
{
//키가 두개 좌우 눌려져있는 상태라면 위 키를 누르면 좌우를 0으로 만든다
if (v != 0)
{
h = 0;
}
}
Vector2 dir = new Vector2(h, v);
Debug.LogFormat("dir: {0}", dir);
this.transform.Translate(dir * this.speed * Time.deltaTime);
//총알 발사
if(this.isShoot)
{
this.delta += Time.deltaTime;
if(this.delta > this.span)
{
this.Shoot();
this.delta = 0;
}
}
}
private void Shoot()
{
Debug.Log("-------------->>> Shoot!!!!!");
var bulletGo = Instantiate<GameObject>(this.bulletPrefab);
bulletGo.transform.position = this.bulletPoint.position;
}
public void StartShoot()
{
Debug.Log("start shoot");
this.isShoot = true;
}
public void StopShoot()
{
Debug.Log("stop shoot");
this.isShoot = false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed = 3f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.Translate(Vector2.up * this.speed * Time.deltaTime);
}
}
'Unity > 수업 내용' 카테고리의 다른 글
[2D]프로젝트Play&plane - 적기 생성 및 사망 (0) | 2021.10.14 |
---|---|
[2D]프로젝트Play&plane - 레이어 된 배경 스크롤링 (0) | 2021.10.14 |
[2D]프로젝트Play&plane - 키보드로 플레이어 이동(중복키 허용) (0) | 2021.10.13 |
[2D]프로젝트Play&plane -키보드 컨트롤 (애니메이션) (0) | 2021.10.13 |
유니티 프로젝트 기획하기(Play&plane) (0) | 2021.10.13 |