using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public Joystick joystick;
public float moveSpeed = 2f;
public GameObject bulletPrefab;
public Transform firePoint;
public float bulletForce = 20f;
public float fireRate = 3f;
public float detectionRadius = 5f; // 공격 범위
public int damage = 1;
private void Start()
{
StartCoroutine(PlayerMovementCoroutine());
StartCoroutine(ShootCoroutine());
}
private IEnumerator PlayerMovementCoroutine()
{
while (true)
{
Vector2 direction = new Vector2(joystick.Horizontal, joystick.Vertical);
transform.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
Vector3 clampedPosition = transform.position; //화면 밖 이동 제한
clampedPosition.x = Mathf.Clamp(clampedPosition.x, -2.8f, 2.8f);
clampedPosition.y = Mathf.Clamp(clampedPosition.y, -5f, 25f);
transform.position = clampedPosition;
if (direction != Vector2.zero)
{
float targetAngle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.eulerAngles = new Vector3(0f, 0f, targetAngle - 90f);
}
else // 조이스틱을 움직이지 않을 때
{
Collider2D[] Enemies = Physics2D.OverlapCircleAll(transform.position, detectionRadius);
Collider2D nearestEnemy = null;
float nearestDistance = detectionRadius;
foreach (Collider2D Enemy in Enemies)
{
if (Enemy.CompareTag("Enemy"))
{
float distanceToEnemy = Vector2.Distance(transform.position, Enemy.transform.position);
if (distanceToEnemy < nearestDistance)
{
nearestDistance = distanceToEnemy;
nearestEnemy = Enemy;
}
}
}
if (nearestEnemy)
{
Vector2 directionToNearestEnemy = nearestEnemy.transform.position - transform.position;
float angle = Mathf.Atan2(directionToNearestEnemy.y, directionToNearestEnemy.x) * Mathf.Rad2Deg;
transform.eulerAngles = new Vector3(0f, 0f, angle - 90f);
}
}
yield return null;
}
}
private IEnumerator ShootCoroutine()
{
while (true)
{
yield return new WaitForSeconds(fireRate);
Vector2 direction = new Vector2(joystick.Horizontal, joystick.Vertical);
// 조이스틱이 움직이지 않을 때만 총알 발사
if (direction == Vector2.zero)
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
if (rb)
{
rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
}
}
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, detectionRadius);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public GameObject Player;
void Update()
{
if (this.Player.transform.position.y > 0 && this.Player.transform.position.y < 20)
{
this.transform.position
= new Vector3(this.transform.position.x, this.Player.transform.position.y, this.transform.position.z);
}
}
}
using UnityEngine;
using System.Collections;
public class WallController : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collider)
{
// 트리거에 들어온 오브젝트의 태그가 "Bullet"인지 확인
if (collider.gameObject.CompareTag("Bullet"))
{
Rigidbody2D rb = collider.gameObject.GetComponent<Rigidbody2D>();
// Rigidbody2D가 있다면 움직임을 멈춤
if (rb)
{
rb.velocity = Vector2.zero;
}
Destroy(collider.gameObject, 2f);
}
}
}
using UnityEngine;
public class EnemyController : MonoBehaviour
{
public int hp; // 적의 체력
[SerializeField] private PlayerController player;
private void OnTriggerEnter2D(Collider2D collider)
{
if (collider.CompareTag("Bullet"))
{
if (player != null)
{
Rigidbody2D rb = collider.gameObject.GetComponent<Rigidbody2D>();
// Rigidbody2D가 있다면 움직임을 멈춤
if (rb)
{
rb.velocity = Vector2.zero;
}
hp -= player.damage; // 플레이어의 공격력만큼 체력 감소
// 체력이 0 이하가 되면 적 제거
if (hp <= 0)
{
OnDestroy();
}
}
}
}
private void OnDestroy()
{
// 현재 오브젝트와 충돌 중인 모든 "Bullet" 태그를 가진 오브젝트를 찾아서 파괴
foreach (Collider2D col in Physics2D.OverlapBoxAll(transform.position, GetComponent<Collider2D>().bounds.size, 0f))
{
if (col.CompareTag("Bullet"))
{
Destroy(col.gameObject);
}
}
Destroy(this.gameObject);
}
}
using UnityEngine;
using System.Collections;
public class BulletDestroy : MonoBehaviour //외벽 (화면 밖 클론 제거)
{
private void OnTriggerEnter2D(Collider2D collider)
{
// 트리거에 들어온 오브젝트의 태그가 "Bullet"인지 확인 후 제거
if (collider.gameObject.CompareTag("Bullet"))
{
Destroy(collider.gameObject);
}
}
}



반응형
'KDT > 유니티 심화' 카테고리의 다른 글
[SpaceShooter] 총알이 벽에 닿았을 때 튕기게 만들기 (0) | 2023.08.21 |
---|---|
[SpaceShooter] 벽에 닿았을 때 캐릭터 회전 시키기 (2) (0) | 2023.08.21 |
[SpaceShooter] 벽에 닿았을때 캐릭터 회전시키기 (1) (0) | 2023.08.21 |
[궁수의 전설 따라해보기] Legend of Circle (0) | 2023.08.21 |
[SpaceShooter] 메인 카메라의 회전과 줌 기능 구현 (0) | 2023.08.17 |