2015-10-14 57 views
0

我有下面的代碼,Camera.ScreenPointToRay沒有做任何

using UnityEngine; 
using System.Collections; 

public class Catch : MonoBehaviour { 
    public float distance; 
    GameObject exterminator; 
    GameObject exterminatorCameraObject; 
    Camera exterminatorCamera; 
    public bool isCarryingPickupableObject = false; 
    public bool stepone, steptwo, stepthree,stepfour; 
    GameObject carriedObject; 
    // Use this for initialization 
    void Start() { 
     stepone = steptwo = stepthree = stepfour= false; 
     exterminator = GameObject.FindWithTag("Exterminator"); 
     exterminatorCameraObject = GameObject.FindWithTag("ExterminatorCamera"); 
     exterminatorCamera = exterminatorCamera.GetComponent<Camera>(); 
    } 

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

     if (isCarryingPickupableObject) 
     { 
      carry(carriedObject); 
      checkDrop(); 
     } 
     else 
     { 
      pickup(); 
     } 

    } 
    void carry(GameObject o) 
    { 
     o.GetComponent<Rigidbody>().isKinematic = true; 
     o.GetComponent<Transform>().position = exterminatorCameraObject.transform.position + exterminatorCameraObject.transform.forward * distance; 

    } 
    void pickup() 
    { 
     stepone = true; 
     if (Input.GetKeyDown(KeyCode.G)) 
     { 
      steptwo=true; 
      //Determine middle of screen for pickup/catch raycast. 
      int x = Screen.width/2; 
      int y = Screen.height/2; 
      Ray ray = exterminatorCamera.ScreenPointToRay(new Vector3(x, y)); 
      RaycastHit hit; 
      stepthree = true; 
      if (Physics.Raycast(ray,out hit)) 
      { 
       stepfour = true; 
       Pickupable p = hit.collider.GetComponent<Pickupable>(); 
       if (p != null) 
       { 
        isCarryingPickupableObject = true; 
        carriedObject = p.gameObject; 
        p.gameObject.GetComponent<Rigidbody>().isKinematic = true; 
       } 
      } 
     } 
    } 
    void checkDrop() 
    { 
     if (Input.GetKeyDown(KeyCode.G)) 
     { 
      dropObject(); 
     } 
    } 
    void dropObject() 
    { 
     isCarryingPickupableObject = false; 
     carriedObject.gameObject.GetComponent<Rigidbody>().isKinematic = false; 
     carriedObject = null; 

    } 
} 

但是在我pickup功能,我GetKeyDown通話從未發生過?

這是爲什麼? (布爾人從來沒有改變,我用它來觀看這個)。

作爲說明:stepone成爲現實,但沒有其他步驟。

編輯:

我又一步,這似乎是它對steptwo但沒有進一步...

編輯:看樣子ScreenPointToRay沒有做任何事情...?

+0

是'exterminatorCameraObject'空? – maksymiuk

回答

1

有幾件事情我可以指出:

  1. 您聲明Vector3,你只給它一個xy
  2. 即使你可以傳遞一個Vector2ScreenPointToRay(...),我強烈建議使用一個Vector3,作爲 函數使用z爲距離攝像頭,並推斷z 0f的距離使得它非常難以推斷出你的Ray
  3. 嘗試使用camera.pixelWidthpixelHeight,因爲Screen.widthheight座標可能不一定在相機的視圖中存在 。

因此,繼承人什麼,我會嘗試:

int distance = 100f; 
int x = exterminatorCamera.pixelWidth/2; 
int y = exterminatorCamera.pixelHeight/2; 
Ray ray = exterminatorCamera.ScreenPointToRay(new Vector3(x, y, distance)); 
0

我建議您檢查您的console log是否有錯誤。如果你正在做它爲「steptwo」,你不會使它到「stepthree」唯一的辦法是,如果有一個錯誤。

我試過你的代碼,並指定我自己的相機我能夠到'stepthree',所以我必須假設它沒有得到你的相機,並且null引用錯誤正在阻止你的腳本得到那麼多。

Debug.Log是你的朋友。嘗試插入Debug.Log(exterminatorCamera)以查看是否正在分配相機。您也可以使用Debug.DrawRay來查看ScreenPointToRay函數輸出的光線。