파라미터 int로 설정
center Input =0
left Input =-1
right Input = 1
플레이어의 파워에 따라 총알 3종 발사하기
코루틴 사용
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
public class Player : MonoBehaviour
{
public float speed;
public Vector2 horizBoundary; //이동제한 범위 설정
public Vector2 vertBoundary;
private Rigidbody2D rb;
Animator animator;
public GameObject bulletA;
public GameObject bulletB;
public float power;
void Start()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
void Update()
{
Move();
InCamera();
StartCoroutine( Shoot());
}
void Move()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical");
Vector3 dir = new Vector3(moveX, moveY, 0);
transform.Translate(dir.normalized * speed * Time.deltaTime);
//애니메이션 방향 전환
if (Input.GetButtonDown("Horizontal")|| Input.GetButtonDown("Vertical"))
{
animator.SetInteger("Input", (int)moveX);
}
}
void InCamera()//화면밖으로 나가지않게 하기
{
float clampX = Mathf.Clamp(rb.transform.position.x, horizBoundary.x, horizBoundary.y);
float clampY = Mathf.Clamp(rb.transform.position.y,vertBoundary.x, vertBoundary.y);
rb.transform.position = new Vector2(clampX, clampY);
}
IEnumerator Shoot() // 코루틴으로 변경
{
// 기본총알 발사
if (Input.GetKeyDown(KeyCode.Space))
{
//power아이템을 먹으면 총알 업그레이드
if (power >= 10)
{
GameObject bullet3 = Instantiate(bulletB, transform.position, transform.rotation);
GameObject bulletL = Instantiate(bulletA, transform.position + Vector3.left*0.1f, transform.rotation);
GameObject bulletR = Instantiate(bulletA, transform.position+Vector3.right*0.1f, transform.rotation);
}
else if(power >= 5)
{
GameObject bullet2 = Instantiate(bulletB, transform.position, transform.rotation);
}
else
{
GameObject bullet1 = Instantiate(bulletA, transform.position, transform.rotation);
}
}
yield return new WaitForSeconds(0.2f);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBullet : MonoBehaviour
{
public float speed;
public int damage;
void Update()
{
// 위로 총알 발사
transform.Translate(Vector2.up * speed * Time.deltaTime);
//화면 밖으로 나가면 사라지게
if (transform.position.y > 5.8f)
{
Destroy(gameObject);
}
}
}
'게임프로젝트 > 슈팅게임' 카테고리의 다른 글
유니티 에셋을 이용한 오브젝트 풀링 (0) | 2024.03.20 |
---|---|
ShootingSpace (5) 아이템 획득, 붐 이펙트 (0) | 2024.03.19 |
ShootingSpace (4) UI만들기 (0) | 2024.03.16 |
ShootingSpace (3) 플레이어와 적, 적 총알과 충돌 (0) | 2024.03.12 |
ShootingSpace (2) 적 3종 랜덤 생성, 총알 충돌처리 (0) | 2024.03.09 |