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

public class BamsongiGenerator : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject bamsongiPrefab;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            //화면을 터치 하면 월드공간에서 Ray를 생성함
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //생성된 레이를 에디터에서 출력
            Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.red, 1f);

            //ray.direction.normalized (단위벡터로 변경)
            //길이가 1인 벡터 : 방향
            Vector3 force = ray.direction.normalized * 2000f;
            this.CreateBamsongi(force);
        }
    }

    private void CreateBamsongi(Vector3 force)
    {
        GameObject go = Instantiate(this.bamsongiPrefab);
        BamsongiController controller = go.GetComponent<BamsongiController>();
        //Vector3 tpos = go.transform.position;
        //tpos.x = 518.89f;

        //go.transform.position = tpos;
        controller.Shoot(force);
    }


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

public class BamsongiController : MonoBehaviour
{
    private Rigidbody rBody;
    [SerializeField]
    private float forwardForce = 2000;
    private ParticleSystem effect;

    private void Awake()
    {
        this.rBody = this.GetComponent<Rigidbody>();
        this.effect = this.GetComponent<ParticleSystem>();
        Debug.Log("Awake");
    }

    void Start()
    {
        //this.Shoot();
        Debug.Log("Start");
    }

    public void Shoot(Vector3 force)
    {
        Debug.Log("Shoot");
        //앞으로 힘으로 줘서 이동시킨다 
        //앞 : (0, 0, 1)
        //Vector3.forward (월드좌표)
        //밤송이가 바라보는 앞쪽? (로컬좌표)
        this.rBody.AddForce(force);
    }

    private void OnCollisionEnter(Collision collision)
    {
        //충돌한 대상의 게임오브젝트의 이름 

        if (collision.gameObject.tag == "Target")
        {
            Debug.Log("과녁에 충돌!");
            this.rBody.isKinematic = true;
            this.effect.Play();
        }
    }
}

+ Recent posts