본문 바로가기
Unity/수업내용

[Unity 3D / 유니티 교과서] 7장 예제 Bamsongi

by 왹져박사 2023. 2. 1.
728x90

MainCamera 선택 후 Ctrl + Shift + F : Scene에서 바라보는 화면을 Game View에 출력되도록 Camera 위치 이동

 

Prefab

  •  Position을 (0, 0, 0)으로 변경 (reset) 후 등록하기 : 등록한 위치에서 인스턴스가 생성되기 때문
  • Prefab -우클릭 - Prefab - Unpack : 프리팹을 일반 오브젝트로 변경

 

BamsongiController

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

public class BamsongiController : MonoBehaviour
{
    private Rigidbody rbody;
    private ParticleSystem fx;

    private void Awake()
    {
        this.rbody = this.GetComponent<Rigidbody>();
        this.fx = this.GetComponent<ParticleSystem>();
    }
    void Start()
    {

    }

    public void Shoot(Vector3 force)
    {
        this.rbody.AddForce(force);
    }

    //다른 콜라이더와 충돌했을 때 호출
    private void OnCollisionEnter(Collision collision)
    {
        this.rbody.isKinematic = true;
        this.fx.Play();
    }
}

 

BamsoniGenerate

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

public class BamsongiGenerate : MonoBehaviour
{
    public GameObject prefab;

    void Start()
    {
        
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            var go = Instantiate(this.prefab);
            //go.transform.position = new Vector3(0, 5f, 0);
            var controller =go.GetComponent<BamsongiController>();

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Debug.DrawRay(ray.origin, ray.direction * 100f);

            go.transform.position = ray.origin + new Vector3(0, 0, 2);

            var dir = ray.direction.normalized;     //길이가 1인 방향 벡터(노멀 벡터)

           controller. Shoot(dir*2000);
        }
    }
}

 

 

728x90