2015-10-14 66 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函数输出的光线。