1. 程式人生 > >unity之讓obj旋轉自轉等操作

unity之讓obj旋轉自轉等操作

foreach move dex public 計時 posit nbsp free bst

1.讓cube沿著矩形四個點運動

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

public class cube : MonoBehaviour {

    // Use this for initialization
    private Vector3 vec;
    float time = 0;
    void Start () {
        vec = transform.position;//存取坐標
    }
    int speed = 1;
    float
speed2 = 0; // bool b = true; // Update is called once per frame void Update () { //time += Time.deltaTime;//計時器 //移動 transform.localScale = new Vector3(1,1,1); transform.Translate(new Vector3(0.02f * speed, 0, speed2)); if (transform.position.x >=4
) { speed = 0; speed2 = -0.02f; } if (transform.position.z<- 4) { speed = -1; speed2 = 0; } if (transform.position.x < -4) { speed = 0; speed2 = 0.02f; }
if (transform.position.z >0) { transform.position = vec; speed = 1; speed2 = 0; } //transform.Rotate(0, speed, 0); } }

2.cube之按鍵操作

//transform.Rotate(Vector3.right,50,Space.World);
        自轉// transform.RotateAround(Vector3.right,5);//只有世界坐標下的運動
        transform.RotateAround(Vector3.zero,Vector3.up,5);//圍繞別人轉
        //if (Input.GetKey("w"))
        //{s
        //    transform.Translate(Vector3.forward);//局地坐標(相對坐標)(0,0,1)
        //}
        //if (Input.GetKey("s"))
        //{
        //    transform.Translate(Vector3.back);//世界坐標,絕對坐標(0,0,-1)
        //}
        //if (Input.GetKey("a"))
        //{
        //    transform.Rotate(Vector3.down);//(0,-1,0)
        //}
        //if (Input.GetKey("d"))
        //{
        //    transform.Rotate(Vector3.up);//(0,1,0)
        //}
       // transform.Translate(Vector3.right);//(1,0,0)
       // transform.Translate(Vector3.left);//(-1,0,0)

        //if (Input.GetKey(KeyCode.W))
        //{
        //    Debug.Log("摁下了W鍵");
        //}
        //if (Input.GetKeyDown(KeyCode.W))
        //{
        //    Debug.Log("Down了一下");
        //}
        //if (Input.GetKeyUp(KeyCode.W))
        //{
        //    Debug.Log("Up了一下");
        //}
        //if (Input.GetKey(KeyCode.Space))
        //{
        //    Debug.Log("摁下了空格鍵");
        //}
        //if (Input.GetKeyDown(KeyCode.Space))
        //{
        //    Debug.Log("Down了一下");
        //}
        //if (Input.GetKeyUp(KeyCode.Space))
        //{
        //    Debug.Log("Up了一下");
        //}
        //if (Input.GetMouseButton(0))
        //{
        //    print("持續摁下了鼠標左鍵");
        //}
        //if (Input.GetMouseButtonDown(0))
        //{
        //    print("摁下了鼠標左鍵");
        //}
        //if (Input.GetMouseButtonUp(0))
        //{
        //    print("擡起了鼠標左鍵");
        //}
        //if (Input.GetMouseButton(1))
        //{
        //    print("摁下了鼠標右鍵");
        //}
        //if (Input.GetMouseButton(2))
        //{
        //    print("摁下了鼠標中鍵");
        //}

3.射線檢測

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;//檢測信息
        if (Physics.Raycast(ray,out hit))
        {
            print(hit.point);
            if (hit.collider.name == "Cube")
            {
                hit.collider.gameObject.GetComponent<MeshRenderer>().material.color = Color.red;
            }

        }

4.cube歲鼠標移動

public GameObject obj;
    Vector3 pos;
    bool b = false;
    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update ()
    {
       // transform.LookAt(obj.transform);
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;//檢測信息
        if (Physics.Raycast(ray, out hit))
        {
            if (Input.GetMouseButtonDown(0))
            {
                pos = hit.point;
                transform.LookAt(new Vector3(pos.x, pos.y + 0.5f, pos.z));//lookat
                b = true;
            }
        }
        if (b)
        {
            transform.Translate(0,0,5*Time.deltaTime);
            if (Vector3.Distance(transform.position,pos)<1f)
            {
                b = false;
                print(123);
            }
        }
    }

5.

點擊cube隨機變顏色,掉下去
     using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RayDiao : MonoBehaviour {
    GameObject obj;
    // Use this for initialization
    void Start () {
        
    }
    Color[] gameclor = { Color.black, Color.blue, Color.cyan, Color.green, Color.red, Color.yellow };//定義顏色數組
    //Random num = new Random();
    // Update is called once per frame
    void Update () {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;//檢查信息
        if (Physics.Raycast(ray,out hit))
            {
            if (Input.GetMouseButtonDown(0))//判斷是否點擊了鼠標左鍵
            {

            if (hit.collider.tag.Equals("cube"))//判斷是否點擊的是cube
            {
hit.collider.gameObject.GetComponent<MeshRenderer>().material.color = gameclor[Random.Range(0, gameclor.Length)];//隨機添加顏色

                    //hit.collider.gameObject.GetComponent<Rigidbody>().useGravity = true;//使添加的剛體激活
                    hit.collider.gameObject.AddComponent<Rigidbody>();//增加剛體
                  // obj= hit.collider.gameObject;

            }
            }
        }
        //if (obj != null)//不是空
        //{
        //    obj.transform.Translate(0, -1*Time.deltaTime, 0);//以(-1*Time.deltaTime)的速度下降
        //}

    }
}

6.

添加cube的方法,如消消樂
    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class creatCube : MonoBehaviour {
    //public GameObject cube;
    // Use this for initialization

    Color [] c = { Color.black, Color.blue, Color.clear, Color.cyan, Color.green, Color.red, Color.yellow };
    void Start () {
       /* GameObject obj = Instantiate(Resources.Load("Cube")) as GameObject;*///另一種生成cube方法

        //GameObject obj = Instantiate(cube);//第二種方式生成cube,需要預制體
        //GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);//生成cube
     //   obj.transform.position = Vector3.one;//one表示坐標(1,1,1)
     //obj.transform.rotation = Quaternion.Euler(0, 45, 0);//角度設置
     // obj.transform.localScale = new Vector3(2, 2, 2);//大小設置
        //int index =obj.name.IndexOf("(");//字符串截取
        //obj.name=obj.name.Substring(0, index);

    for (int i = 0; i<10; i++)
            {
            for (int j = 0; j < 10; j++)
            {
                GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);//生成cube
                obj.transform.position = new Vector3(i, j, 0);//生成位置
                obj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);//生成大小
                obj.GetComponent<MeshRenderer>().material.color = c[Random.Range(0, c.Length)];//隨機顏色
            }
            }
    }
    // Update is called once per frame
    void Update () {
        
    }
}

7.

鼠標點擊變大
  Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.collider.gameObject.tag == "cube" &&Input.GetMouseButtonDown(0))
            {
                 cubeObj=  hit.collider.gameObject;
                cubeObj.transform.localScale = new Vector3(1,1,1);
            }        
        }
        if (Input.GetMouseButtonUp(0)&&cubeObj)
        {
            cubeObj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
        }

8.

消消樂--單消————列表方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Raydaxiao : MonoBehaviour {
    GameObject obj;
    public GameObject cube;//為預制體生成cube做準備
    //Color[] c = { Color.black, Color.blue, Color.clear, Color.cyan, Color.green, Color.red, Color.yellow };
    public Material[] color;//材質數組
    Vector3 vec;
    List<GameObject> cubelist = new List<GameObject>();
    //int num = 0;
    // Use this for initialization
    void Start () {
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                GameObject obj = Instantiate(cube);//需要預制體式添加cube
                obj.transform.position = new Vector3(i, j, 0);
                obj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
                //obj.GetComponent<MeshRenderer>().material.color = c[Random.Range(0, c.Length)];//上顏色
                obj.GetComponent<MeshRenderer>().material = color[Random.Range(0, color.Length)];//隨機材質
                obj.tag = "cube";//定義名字為cube
                //obj.AddComponent<Rigidbody>();//添加剛體
                //num++;
                //obj.name = "Cube" + num;//生成不同名字的cube
                cubelist.Add(obj);
            }
        }

    }

    // Update is called once per frame
    void Update () {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if(Physics.Raycast(ray,out hit))
        {
            if (hit.collider.gameObject.tag=="cube"&&Input.GetMouseButtonDown(0))
            {
                obj = hit.collider.gameObject;
                obj.transform.localScale = new Vector3(1, 1, 1);
                vec = obj.transform.position;//獲取當前cube的坐標
                //Destroy(obj);//銷毀cube
                Destroy(obj);
                cubelist.Remove(obj);
                foreach (GameObject item in cubelist)
                {
                    if (item.transform.position.x == vec.x && item.transform.position.y > vec.y)
                    {
                        //item.transform.position -= new Vector3(0, 1, 0);//另一種
                        item.transform.position = new Vector3(vec.x, item.transform.position.y - 1, vec.z);
                    }//自動補齊
                }
                GameObject newobj = Instantiate(cube);//重新生成cube
                newobj.GetComponent<MeshRenderer>().material = color[Random.Range(0, color.Length)];//添加顏色
                newobj.transform.position = new Vector3(vec.x, 9, vec.z);//newcube的坐標
                newobj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
                //newobj.AddComponent<Rigidbody>();//添加剛體
                //newobj.GetComponent<Rigidbody>().freezeRotation = true;
                //newobj.GetComponent<BoxCollider>().size = new Vector3(1.1f, 1.1f, 1.1f);

                cubelist.Add(newobj);
            }
            //if (Input.GetMouseButtonUp(0)&&obj)
            //{

            //    obj.transform.localScale = new Vector3(0.9f,0.9f,0.9f);
            //}
        }
    }
}

技術分享圖片

9.

消消樂--單消--數組方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TextScripts : MonoBehaviour
{
    // List<GameObject> list = new List<GameObject>();
    GameObject[,] array = new GameObject[10,10];
    bool b = false;
    GameObject cube;
    Color[] c = { Color.red,Color.blue,Color.yellow,Color.green};
   public Material[] m;
    // Use this for initialization
    void Start()
    {
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
                obj.transform.position = new Vector3(i, j, 0);
                obj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
                obj.tag = "cube";
                obj.GetComponent<MeshRenderer>().material = m[Random.Range(0,3)];
                array[i, j] = obj;
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray,out hit))
        {
            if (hit.collider.tag=="cube"&&Input.GetMouseButtonDown(0))
            {
                cube = hit.collider.gameObject;
                Vector3 pos = cube.transform.position;
                int a = (int)pos.x;
                int b = (int)pos.y;
                Destroy(cube);              
                for (int i = b+1; i < 10; i++)//從銷毀的上邊第一個開始
                {
                    array[a, i].transform.position -= new Vector3(0,1,0);/到比銷毀物體坐標的y大的物體把其y-1
                    array[a, i - 1] = array[a,i];//坐標統一減去1,導致索引對應物體改變
                }

                GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
                obj.transform.position = new Vector3(pos.x, 9, 0);
                obj.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
                obj.tag = "cube";
                obj.GetComponent<MeshRenderer>().material = m[Random.Range(0, 3)];
                //  list.Add(obj);
                array[a, 9] = obj;
            }
        }
       
    }
}

10.

技能冷卻:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CoolSkill : MonoBehaviour
{
    public Image image;
    float time;
    float f;
    public Text text;
    bool b = false;
    bool bb = true;
    // Use this for initialization
    void Start()
    {
        image = image.GetComponent<Image>();
        text = text.GetComponent<Text>();
        image.fillAmount = 0;//默認可以發出技能
    }
    public void GetBool()
    {
        if (bb)//限制技能開啟後才能使用
        {
            b = true;
            bb = false;
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (b)
        {
            time += Time.deltaTime;
            if (time <= 5)//技能控制在5秒冷卻
            {
                f = (5 - time);//5秒倒計時
                image.fillAmount = (f) / 5;//image也在360度遞減
                text.text = (f).ToString();//文本輸出倒計時
                if (f < 0.1f && f >= 0)/控制在0.1秒以內結束時才可以重新開啟技能
                {
                    bb = true;//重新開啟技能Button可以點擊了
                }
            }
            else
            {
                time = 0;//超過5秒後時間置零
                b = false;/tton點擊後又可以計時了
            }


        }

    }
}

unity之讓obj旋轉自轉等操作