Unity射線,鼠標點擊


WALLdEMO:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;  
public class WALLdEMO : MonoBehaviour {

    // Use this for initialization
    public float sx;
    public float sy;
    public float ex;
    public float ey;
    public Transform BarrierParent;

    //預制體:預先制作好的游戲對象
    public GameObject Barrier1;
    void Start () {
        CreateBarrier();
    }
    
    // Update is called once per frame
    void Update () {
        
    }
    void CreateBarrier() {
        for (float i = sx; i <= ex; i += 1.55f) {
            for (float j = sy; j>= ey; j -= .6f)
            {
                GameObject barrier = GameObject.Instantiate(Barrier1);//克隆
                barrier.transform.position = new Vector3(i, j, BarrierParent.position.z);
                barrier.transform.parent = BarrierParent;
                //隨機顏色,得到不同等級障礙物
                int index = Random.Range(0,Barrier.ColorArr.Length-1);
                barrier.GetComponent<Barrier>().OnChangeColor(index);
                barrier.GetComponent<Barrier>().HP = index + 1;
            }
        
        }
    }
}

WoodControl:

using UnityEngine;
using System.Collections;

public class WoodControl : MonoBehaviour {

    // Use this for initialization
    public Transform LP;
    public Transform RP;
    void Start () {
    
    }
    
    // Update is called once per frame
    //float nx=0;
    void Update () {
        float d = Input.GetAxis ("Horizontal");
        this.transform.Translate (d*12*Time.deltaTime,0,0);
        if (this.transform.position.x <= LP.position.x+1.5f)
            this.transform.position = new Vector3(LP.position.x+1.5f, this.transform.position.y, this.transform.position.z);
        if (this.transform.position.x >= RP.position.x-1.5f)
            this.transform.position = new Vector3(RP.position.x-1.5f, this.transform.position.y, this.transform.position.z);
    }

    
}





cube:

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

public class cube : MonoBehaviour {
    Rigidbody rg;
    bool isDead;
    // Use this for initialization
    void Start () {
        //記得加剛體
        rg = this.GetComponent<Rigidbody>();
        isDead = true;
        //給予一個初速度
        rg.velocity = Vector3.up * 3;

    }

    // Update is called once per frame
    void Update () {
        //
        //if (Input.GetMouseButtonDown(0))     
            if (isDead && Input.GetButtonDown("Jump")) 
             {
                //添加一個速度
                    rg.velocity = Vector3.up * 5;
            //添加一個力
            // rg.AddForce(Vector3.up * 300);
        }

    }
}
 

shoot:

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

public class shoot : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    public GameObject bullet1;
     //public Transform bullet1;//????只是需要這個物體上的位置信息 ,而不是這個物體
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            //創建子彈
            CreateBullet();
        }
    }
    //剛體是組件 組件訪問 GetCompon/ent
    void CreateBullet()
    {
        //創建一個指定類型的3D對象
        GameObject bullet = GameObject.CreatePrimitive(PrimitiveType.Cube);
        //初始化 子彈的位置
        bullet.transform.position = bullet1.transform.position;
        //改變大小
        bullet.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
        //加上剛體的組件
        // bullet.AddComponent("Rigibody "); GameObject.AddComponent(string);已過時
        Rigidbody rg = bullet.AddComponent<Rigidbody>();
        //Transform  屬性 forward返回該對象的正前方方向向量,Z正前方
        // rg.velocity = bullet1.forward * 10;  若定義的bullet1 定義的是Transform   
        rg.velocity = bullet1.transform.forward * 10;
    }
}

RayDemo:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RayDemo : MonoBehaviour
{
    GameObject currentClick;
    void Start()
    {
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            //256:2的(層次)冪
            //
           int  LayerIndex = LayerMask.NameToLayer("star");
            if (Physics.Raycast(ray, out hit, int.MaxValue,/*256*/ LayerIndex)) 
            {
                currentClick = hit.collider.gameObject;
                print("點擊到一個對象");
                CastUp();
            }
        }
    }
     
    void CastUp()
    {
        if (currentClick != null)
        {
            print("向上發射射線");
            
            Ray r = new Ray(currentClick.transform.position - new Vector3(0, 0.5f, 0), Vector3.up);
            //Ray r = new Ray(currentClick.transform.position + new Vector3(0, 0.5f, 0), Vector3.left);
            RaycastHit hit;
            
            if (Physics.Raycast(r, out hit, 0.5f, 256))
            {
                print("檢測到一個對象");
                //檢測到的上面對象
                GameObject obj = hit.collider.gameObject;
                //獲取組件網格渲染                                                                                                                                                                                                         
                MeshRenderer mr = obj.GetComponent<MeshRenderer>();
                //獲取該對象顏色
                Color c = mr.material.color;
                //獲取點擊對象的顏色
                Color currentC = currentClick.GetComponent<MeshRenderer>().material.color;
                //判斷顏色是否一樣
                if (c.Equals(currentC))
                {
                    //刪除
                    Destroy(obj);
                    Destroy(currentClick);
                    obj = null;
                    currentClick = null;
                }
            }

        }
    } 
}
/*
 Ray:射線,起點和方向
 * RaycastHit:存儲射線碰撞碰撞的信息;
 * Phyics.Raycast(Ray,out RaycastHit)用來進行碰撞檢測
 * Phyics.Raycast(Ray,out RaycastHit,float maxDistance,int layermask)用來進行碰撞檢測
 * 結構存在棧里面,值類型
 * 引用類型的對象存在堆
 * 
 * 案例講:向上發射射線檢測
 * 1.上下左右,只要檢測到相同的顏色的格子就刪除
 * 2.Layermask
 * 3.剛體
 */
/*
            * 射線檢測主要是檢測射線是否跟場景中碰撞體發生碰撞,相交
            進行射線檢測,發生碰撞,就會碰撞信息存到hit里面;
            * 同時返回true,反之返回false,hit還是空的;
            * Raycast(Ray,out RaycastHit)
            * Raycast(Ray,out RaycastHit,float maxDistance,int layermask)
            */

test:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //unity 如何將屏幕坐標轉換為射線
            //將屏幕坐標轉換為射線
            //當前鼠標位置的像素坐標:Input.mousePosition
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //存儲射線碰撞的信息(存儲坐標 碰到的物體的信息)
            //面試
            RaycastHit hit;//值變量
            //只能和物體碰撞 在物理系統。若碰撞 返回true.發生碰撞,hit中會存入碰撞信息。
            //沒有碰撞器,射線會穿過
            //射線檢測:if (Physics.Raycast(ray,out hit))長度 2的層次方
            /*  Raycast(Ray,out RaycastHit)
             * Raycast(Ray,out RaycastHit,float maxDistance,int layermask)
                   */
            //Raycast 
            if (Physics.Raycast(ray, out hit, int.MaxValue, 256))
            {
                //print("發生碰撞,鼠標訪問到對象");
                //獲取對象
                // print:調試用
                //print("相交位置:" + hit.point);
                //print("碰撞的對象:" + hit.collider); 
                currentClick = hit.collider.gameObject;
                print("點擊到一個對象");
                CastUp();
            }

        }

    }

     GameObject currentClick;
    void CastUp()
    {
        
        if (currentClick != null)
        {
            print("向上發射射線");
            //currentClick.transform.position:中心點
            //當前位置開始 發射一條向上的射線
            //new Vector3(0,0.5f,0)????
            Ray r = new Ray(currentClick.transform.position - new Vector3(0, 0.5f, 0), Vector3.down);
            RaycastHit hit;
            //Ray s = new Ray(currentClick.transform.position + new Vector3(0, 0.5f, 0), -Vector3.up);
            //RaycastHit hit1;
            if (Physics.Raycast(r, out hit, 0.5f, 256))
            {
                print("檢測到一個對象");
                //判斷顏色 返回物體組件
                //檢測到的上面的對象                
                GameObject obj = hit.collider.gameObject;
                //GameObject obj1 = hit.collider.gameObject;
                //除了Transform 其他組件都可以使用這個泛型獲得
                //獲得組件網格渲染
                MeshRenderer mr = obj.GetComponent<MeshRenderer>();
                print("顏色:" + mr.material.color);
                //object :是所有的基類
                //獲取該對象顏色
                Color a = mr.material.color;
                //獲取該點擊對象的顏色
                Color A = currentClick.GetComponent<MeshRenderer>().material.color;
                //判斷顏色是否一樣
                if (a.Equals(A))
                {
                    //刪除
                    Destroy(obj);
                    Destroy(currentClick);
                    obj = null;
                    currentClick = null;
                }


            }
        }
    } 
              void check()
                {

                } 
}
/*結構存在棧里,值類型
 結構:所有數據類型 都是結構類型
 射線:起點 方向
  Ray:射線,起點和方向
 * 
 * 結構存在棧里面,值類型
 * 引用類型
TAG:是main camera,才是主攝像機
     */



_413:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class _413 : MonoBehaviour {
    //鼠標點擊移動
    // Use this for initialization
    void Start () {
        MoveEndPos=this.transform.position;
    }
    //Vector3定義時,不需要賦初始值.Vector3:三維坐標
    Vector3 MoveEndPos;
    // Update is called once per frame
    void Update () {
        //左鍵摁下
        if(Input.GetMouseButtonDown(0))
        {
            //定義一射線
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            
            //計算射線的起點和終點。
            RaycastHit hit;
            if(Physics.Raycast(ray,out hit))
            {
                MoveEndPos = hit.point;
            }
        }
        this.transform.LookAt(MoveEndPos);//朝向問題
        this.transform.position = Vector3.MoveTowards(this.transform.position,  MoveEndPos, 0.05f);//第三個參數:移動速度,每一幀移動的距離
        
    }
}


createcube:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class createcube : MonoBehaviour {

    // Use this for initialization
    void Start () {

        //Invokes the method methodName in time seconds, then repeatedly every repeatRateseconds.在時間 秒 中調用方法方法名稱,然后重復每個重復頻率秒。

        //public void InvokeRepeating(string methodName, float time, float repeatRate);
        InvokeRepeating("create", 5f, Time.deltaTime * 40);
    }
     
    public  GameObject obj;
    // Update is called once per frame
    void Update () {
       // create();
    }
   // public GameObject cube;
    void create()
    {
        //if (Input.GetMouseButtonDown(0))
        //{
        //    float x = Random.Range(-5f, 5f);
        //    float z = Random.Range(-5f, 5f);
        //    //obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
        //    obj = (GameObject)GameObject.Instantiate(obj);
        //    obj.transform.Translate(x, -1.3f, z);
        //    //若外邊  private Transform obj1;
        //    // 可以 obj1.position = new Vector3(0, 0, 0);
        //}
        for (int count = 0; count < Random.Range(0, 2); count++)
        {
            //object
            Instantiate(obj, new Vector3(count, 1.3f, count), Quaternion.identity);
            //Quaternion.identity :The identity rotation(Read Only).標識旋轉(只讀)。
        }

    }
     
}



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

public class copy : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray,out hit))
            {
                print("觸碰到物體");
                print("位置:" + hit.point);
            }
        }
       
        
    }
}


OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.

當 collider/rigidbody停止觸動另一個 rigidbody/collider時,OnCollisionExit被調用。

In contrast to OnTriggerExit, OnCollisionExit is passed the Collision class and not a Collider. The Collision class contains information about contact points, impact velocity etc. If you don't use collisionInfo in the function, leave out the collisionInfo parameter as this avoids unneccessary calculations. Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.

相比OnTriggerExit,OnCollisionExit傳遞Collision類而不是Collider。Collision類包含接觸點,碰撞速度等信息。在這個函數如果你不使用collisionInfo,刪去collisionInfo參數以避免不必要的計算。注意,當其中至少一個碰撞盒附加非動力學剛體時碰撞事件才會發送。


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

public class cooperate : MonoBehaviour {

    // Use this for initialization
    void Start () {
        print(Time.deltaTime);
    }


    // Update is called once per frame
     public bool isRotate = false;
    void Update () {
        //Jump:空格
        if (Input.GetButtonDown("Jump"))
        {
            isRotate = !isRotate;
        }
        Rotate();
        RandomPosition();
    }
    void Rotate()
  {
        if (isRotate)
        {
            this.transform.Rotate(0, 1, 0);
        }
    }
    //if (Input.GetKeyDown(KeyCode.Space))
    //{
    //    jump();
    //}
    //else if (Input.GetMouseButtonDown(1)|| Input.GetMouseButtonDown(0)|| Input.GetMouseButtonDown(2))
    //{
    //    this.transform.position += new Vector3(0,0,0);
    //Random a = new Random();
    //}
    void RandomPosition()
    {
        //任意移動在平面上
        if (Input.GetButtonDown("Fire1"))
        {
            float x = Random.Range(-5f,5f);
            float z = Random.Range(-5f,5f);
            this.transform.position = new Vector3(x, 1.02f, z);

        }

        //else if (Input.GetKey(KeyCode.Space))
        //{
        //    jump();
        //}
        //KeyCode.Space:空格鍵
    }

    //Input




    void  jump()
    {
        //localScale:大小
        this.transform.position += new Vector3(0, 1, 0);
    }
    void CreateCube()
    {
        //PrimitiveType Cube;  
        //GameObject.CreatePrimitive();
    }


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

public class ActionOfMouse : MonoBehaviour {

    // Use this for initialization
    void Start () {
        //打印電腦幀數
        print(Time.deltaTime);
    }
    //Transform
    // Update is called once per frame
    void Update () {

        OnMouseEnter();
        print("鼠標在對象上"); 
         
    }
      void OnMouseUp()
    {
        this.transform.localScale -= new Vector3(1, 1, 1);
    }

    void OnMouseEnter()
    {
        this.transform.localScale = 2*new Vector3(1, 1, 1);
    }
}


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

public class ratation : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    public Transform targetobject;
    public float speed = 1f;//旋轉速度
    // Update is called once per frame
    void Update () {
        transform.RotateAround(targetobject.position, Vector3.up, speed);
    }
    
}


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

public class cooperate : MonoBehaviour {

    // Use this for initialization
    void Start () {
        print(Time.deltaTime);
    }


    // Update is called once per frame
     public bool isRotate = false;
    void Update () {
        //Jump:空格
        if (Input.GetButtonDown("Jump"))
        {
            isRotate = !isRotate;
        }
        Rotate();
        RandomPosition();
    }
    void Rotate()
  {
        if (isRotate)
        {
            this.transform.Rotate(0, 1, 0);
        }
    }
    //if (Input.GetKeyDown(KeyCode.Space))
    //{
    //    jump();
    //}
    //else if (Input.GetMouseButtonDown(1)|| Input.GetMouseButtonDown(0)|| Input.GetMouseButtonDown(2))
    //{
    //    this.transform.position += new Vector3(0,0,0);
    //Random a = new Random();
    //}
    void RandomPosition()
    {
        //任意移動在平面上
        if (Input.GetButtonDown("Fire1"))
        {
            float x = Random.Range(-5f,5f);
            float z = Random.Range(-5f,5f);
            this.transform.position = new Vector3(x, 1.02f, z);

        }

        //else if (Input.GetKey(KeyCode.Space))
        //{
        //    jump();
        //}
        //KeyCode.Space:空格鍵
    }

    //Input




    void  jump()
    {
        //localScale:大小
        this.transform.position += new Vector3(0, 1, 0);
    }
    void CreateCube()
    {
        //PrimitiveType Cube;  
        //GameObject.CreatePrimitive();
    }


}




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

public class ratation : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    public Transform targetobject;
    public float speed = 1f;//旋轉速度
    // Update is called once per frame
    void Update () {
        transform.RotateAround(targetobject.position, Vector3.up, speed);
    }
    
}




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

public class self : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }
    //自轉
    //public Transform oneself;
    public float speed = 1f;
    // Update is called once per frame
    void Update()
    {
        //this.transform.Rotate(0,0.02f,0);
        //Vector3.up :0,1,0
        //Vector3.zero:0,0,0
        transform.Rotate(Vector3.up * speed, Space.World);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM