0407 점수 쌓이는 ui, 랜덤 발생 오브젝트, 클릭하는 곳으로 이동

2021. 4. 8. 09:41unity

raycast 쏘는 법 추가

using UnityEngine;

public class ItemController : MonoBehaviour
{
    public float dropSpeed = 0;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.Translate(0, -0.03f, 0);
        if (this.transform.position.y < -1.0f) 
        {
            Destroy(gameObject);
        }
    }
}​
using UnityEngine;

public class BasketController : MonoBehaviour
{
    public AudioClip apple_se;
    public AudioClip bomb_se;
    private AudioSource audio;
    GameObject director;
    // Start is called before the first frame update
    void Start()
    {
        this.audio = GetComponent<AudioSource>();
        //this.director = GameObject.FindObjectOfType<GameDirector>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0)) 
        {
            var ray=Camera.main.ScreenPointToRay(Input.mousePosition);
            Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 1);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, Mathf.Infinity)) 
            {
                float x = Mathf.RoundToInt(hit.point.x);
                float z = Mathf.RoundToInt(hit.point.z);
                this.transform.position = new Vector3(x, 0, z);
            }
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "apple")
        {
            this.audio.PlayOneShot(this.apple_se);
        }
        else 
        {
            this.audio.PlayOneShot(this.bomb_se);
        }
        Debug.Log("잡았다!");
        if (other.gameObject.tag == "apple") 
        {
            Debug.Log("Apple");
        }
        else 
        {
            Debug.Log("Bomb");
        }
        Destroy(other.gameObject);
    }
}
using UnityEngine;

public class ItemGenerator : MonoBehaviour
{
    public GameObject applePrefab;
    public GameObject bombPrefab;
    float span = 1.0f;
    float delta = 0;
    int ratio = 2;
    float speed = -0.03f;
    public void SetParameter(float span, float speed, int ratio)
    {
        this.span = span;
        this.speed = speed;
        this.ratio = ratio;
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }
    
    // Update is called once per frame
    void Update()
    {
        this.delta += Time.deltaTime;
        if (this.delta > this.span) 
        {
            this.delta = 0;
            GameObject item;
            int dice = Random.Range(1, 11);
            if (dice <= this.ratio)
            {
                item = Instantiate(bombPrefab) as GameObject;
            }
            else 
            {
                item = Instantiate(applePrefab) as GameObject;
            }
            
            float x = Random.Range(-1, 2);
            float z = Random.Range(-1, 2);
            item.transform.position = new Vector3(x, 4, z);

            ItemController controller = item.GetComponent<ItemController>();
                
                
                controller.dropSpeed = this.speed;
        }
        
    }
}
using UnityEngine;
using UnityEngine.UI;

public class GameDirector : MonoBehaviour
{
    GameObject timeText;
    float time = 60.0f;
    GameObject pointText;
    int point = 0;
    // Start is called before the first frame update
    public void GetApple() 
    {
        this.point += 100;
    }
    public void GetBomb() 
    {
        this.point /= 2;
    }
    void Start()
    {
        this.timeText = GameObject.Find("timeText");
        this.pointText = GameObject.Find("Point");
    }

    // Update is called once per frame
    void Update()
    {
        this.time -= Time.deltaTime;
        this.timeText.GetComponent<Text>().text = this.time.ToString("F1");
        this.pointText.GetComponent<Text>().text = this.point.ToString() + "point";
    }
}