유니티 기초

챕터7 밤송이 [수정필]

다모아 2023. 8. 4. 12:50

Center, Pivot 모드

 

씬 뷰에서 바라보는 시선으로 카메라(오브젝트 방향) 바꾸기 : Ctrl + Shift + F

 

색칠하기

Texture(그림), 매터리얼 필요

asset store

 

 

BamsongiController

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

public class BamsongiController : MonoBehaviour
{
    private Rigidbody rBody;
    [SerializeField]
    private float forwardForce = 2000;
    private ParticleSystem effect;
    // Start is called before the first frame update
    private void Awake()
    {
        this.rBody = this.GetComponent<Rigidbody>();
        this.effect = this.GetComponent<ParticleSystem>();
        Debug.Log("Awake");
    }

    void Start()
    {

        Debug.Log("start");
    }

    public void Shoot(Vector3 force)
    {
        Debug.Log("Shoot");
        //앞으로 힘으로 줘서 이동시킨다
        //앞 ( 0, 0, 1);, Vector3.forward;
        //this.rBody.AddForce(방향 * 힘);
        this.rBody.AddForce(force);

    }

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

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

 

BamsongiGenerator

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

public class BamsongiGenerator : MonoBehaviour
{
    public GameObject bamsongiPrefab;
    // Start is called before the first frame update
    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>();
        controller.Shoot(force);
    }
}

 

..카메라 위치에서 나가도록 수정해야함