using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float speed = 1f;
public Rigidbody playerRigidbody;
public GameObject bulletPrefab;
void Update()
{
PlayerMove();
}
private void PlayerMove()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(h, 0, v) * speed;
transform.Translate(movement * Time.deltaTime);
}
private void OnTriggerEnter(Collider other)
{
// 충돌한 오브젝트가 총알인지 확인
if (other.CompareTag("Bullet"))
{
// 플레이어를 파괴
Destroy(gameObject);
Debug.Log("Game over");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] GameObject player;
[SerializeField] public GameObject bulletPrefab;
private float spawnInterval = 3f; // 총알 생성 간격
private Vector3 spawnPosition; // 총알이 생성될 위치
void Start()
{
spawnPosition = this.gameObject.transform.position; // 총알이 생성될 위치 초기화
StartCoroutine(SpawnBullet()); // 코루틴 시작
}
IEnumerator SpawnBullet()
{
while (player != null)
{
// 총알 생성
Instantiate(bulletPrefab, spawnPosition, Quaternion.identity);
// 일정한 간격 기다리기
yield return new WaitForSeconds(spawnInterval);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed = 2f;
public GameObject player;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
// Rigidbody 컴포넌트 가져오기
Rigidbody bullets = GetComponent<Rigidbody>();
Vector3 direction = (player.transform.position - transform.position).normalized;
transform.forward = direction;
// 총알의 이동 방향 설정 및 속도 적용
bullets.velocity = transform.forward * speed;
// 5초 후에 총알 파괴
Destroy(gameObject, 4.5f);
}
private void OnTriggerEnter(Collider other)
{
// 충돌한 오브젝트가 총알인지 확인
if (other.CompareTag("Player"))
{
Destroy(gameObject);
}
if (other.CompareTag("Wall"))
{
Destroy(gameObject);
}
}
}
'산대특 > 게임 클라이언트 프로그래밍' 카테고리의 다른 글
UniRun (4) | 2024.03.05 |
---|---|
Quaternion (1) | 2024.03.04 |
코루틴 연습 (0) | 2024.03.03 |
StarCraft - Tank (0) | 2024.02.29 |
StarCraft - DropShip Logic (0) | 2024.02.28 |