아카이브
총알발사 본문
BulletController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletController : MonoBehaviour
{
[SerializeField]
private float force = 1500f;
private Rigidbody rBody;
// Start is called before the first frame update
void Start()
{
this.rBody = this.GetComponent<Rigidbody>();
//방향 * 힘
//월드 좌표 기준으로 힘이 가해짐
this.rBody.AddForce(Vector3.forward * this.force);
}
FireController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireController : MonoBehaviour
{
//총알 프리팹
public GameObject bullet;
//총알 발사 좌표
public Transform firePose;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
Fire();
}
}
void Fire()
{
//Bullet프리팹을 동적으로 생성(생성객체, 위치, 회전)
Instantiate(bullet, firePose.position, firePose.rotation);
}
}
RemoveBullet
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RemoveBullet : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
Debug.Log(collision);
if (collision.collider.CompareTag("Bullet"))
{
Destroy(collision.gameObject); //bullet
}
//if (collision.collider.tag == "Bullet")
//{
// Destroy(collision.gameObject); //bullet
//}
}
}
결과
'유니티 심화' 카테고리의 다른 글
HeroShooter 적이 플레이어 추적 (0) | 2023.08.29 |
---|---|
Hero Shooter 문열기, 애니메이션 적용 (0) | 2023.08.24 |
Hero Shooter 조이스틱 이동, 포탈생성 (0) | 2023.08.23 |
8/20 주말과제 궁수 이동, 공격 (0) | 2023.08.21 |
캐릭터 이동 (0) | 2023.08.17 |