유니티 심화

챕터5 - 총알

다모아 2023. 8. 18. 16:34
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(this.transform.forward * force);
        //this.rBody.AddRelativeForce(Vector3.forward * force);
        //월드좌표기준으로 힘이 가해짐
        this.rBody.AddForce(Vector3.forward * force);
    }

    // Update is called once per frame
    void Update()
    {

    }
}

y축으로 회전해도 월드축의 z좌표로 날아간다


FireController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FireController : MonoBehaviour
{
    [SerializeField]
    private GameObject bulletPrefab;
    [SerializeField]
    private Transform firePoint;
    
    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            this.Fire();
        }
    }

    private void Fire()
    {
        //Bullet 프리팹의 인스턴스를 생성한다.
        //Instantiate(this.bulletPrefab);

        //프리팹, 부모 (Transform)
        //Instantiate(this.bulletPrefab, parent);

        //프리팹, 위치, 회전
        Instantiate(this.bulletPrefab, this.firePoint.position, this.firePoint.rotation);
    }
}

 

FireGizmos

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FireGizmos : MonoBehaviour
{
    [SerializeField]
    private Color color = Color.yellow;
    [SerializeField]
    private float radius = 0.1f;
    private void OnDrawGizmos()
    {
        Gizmos.color = this.color;
        Gizmos.DrawSphere(this.transform.position, radius);
    }
}

 

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"))
        //{

        //}
        if(collision.collider.tag == "Bullet")
        {
            Destroy(collision.gameObject);
        }
    }
}

 

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(this.transform.forward * force);
        this.rBody.AddRelativeForce(Vector3.forward * force);
        //월드좌표기준으로 힘이 가해짐
        //this.rBody.AddForce(Vector3.forward * force);
    }

    // Update is called once per frame
    void Update()
    {

    }
}

p238

 

'유니티 심화' 카테고리의 다른 글

[주말과제] - 궁수의 전설[완]  (0) 2023.08.19
SpaceShooter2D - 스페이스바 눌러서 총쏘고 제거하기 [완]  (0) 2023.08.18
Ref  (0) 2023.08.18
Lerp, Slerp  (0) 2023.08.18
캐릭터 애니메이션 추가  (0) 2023.08.18