Unity通過射線快速變化物體顏色

using UnityEngine;
using System.Collections;


public class Task : MonoBehaviour {

    Ray ray;
    RaycastHit _hit;

    GameObject cube;
    Material color1;

    // Use this for initialization
    void Start () {
        //創建一個方塊
        cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

        color1= GameObject.Find("Cube").GetComponent<MeshRenderer>().material;
    }

    // Update is called once per frame
    void Update () {

       //獲取鼠標射線
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out _hit))
        {
            if (_hit.transform.gameObject.GetComponent<MeshRenderer>())
            {
               //隨機一種顏色
                Color color = new Color(Random.Range(0f, 1f) * Time.deltaTime * 30f, Random.Range(0f, 1f) * Time.deltaTime * 30f, Random.Range(0f, 1f) * Time.deltaTime * 30f);
                //給方塊添加上顏色
                color1.color = color;
            }
        }

    }


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章