2016-05-01 43 views
0

本遊戲中的相機具有可以通過點擊其他模型進行更改的目標,然後是相機焦點,但下面的腳本是我目前爲止所獲得的目標當我點擊遊戲中的一個對象時,目標只是說沒有任何模型。C#unity使用光線投射命中信息更改變量

Ray toMouse = Camera.main.ScreenPointToRay(Input.mousePosition); 
RaycastHit hitInfo; 
bool didHit = Physics.Raycast(toMouse, out hitInfo); 

if (didHit) 
{ 
    if (hitInfo.collider.tag == "Cell" && Input.GetMouseButtonDown(0)) 
    { 
     Debug.Log("Cell hit"); 

     target = hitInfo.transform.Find(gameObject.name);  
    } 
} 
+0

什麼是'gameObject.name'?似乎在這裏缺少一些代碼。 –

+0

它的工作原理是將射線命中的對象的名稱轉換爲目標名稱。 – ryand444

回答

1

如果此腳本是在相機上,這樣的事情應該這樣做:

GameObject target; 
// or 
Transform target; 

void Update() 
{ 
    if(Input.GetMouseButtonDown(0)) 
    { 
     RaycastHit hit; 
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
     if(Physics.Raycast(ray, out hit)) 
     { 
      target = hit.transform.gameObject; 
      // or 
      target = hit.transform; 
     } 
    } 
} 
+0

非常感謝你,我已經學會了我做錯了什麼,現在它工作正常。 – ryand444

+0

@ ryand444。對你有好處。請接受答案,因爲它對你有幫助。 – Programmer