0407 3d 밤송이 던지기

2021. 4. 7. 16:12unity

using UnityEngine;

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

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0)) 
        {
            //프리팹으로 가지고 게임 오브젝트 생성
            var go = Instantiate<GameObject>(bamsongiPrefeb);
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 1);
            var dir = ray.direction;
            go.GetComponent<BamsongiController>().Shoot(dir.normalized * 2000);
        }
        
    }
}
using UnityEngine;

public class BamsongiController : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //this.Shoot(new Vector3(0, 200, 2000));    
    }

    // Update is called once per frame
    public void Shoot(Vector3 dir)
    {
        this.GetComponent<Rigidbody>().AddForce(dir); //방향+힘
    }
    private void OnCollisionEnter(Collision collision)
    {
        GetComponent<Rigidbody>().isKinematic = true; //중력 영향을 받지 않도록
        GetComponent<ParticleSystem>().Play();
    }


}