Camera.ScreenPointToRay没有做任何事情

时间:2015-10-14 03:12:47

标签: c# input unity3d

我有以下代码,

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没有做任何事情......?

2 个答案:

答案 0 :(得分:1)

我可以指出一些事情:

  1. 您声明了Vector3,并且只提供了xy
  2. 即使您可以将Vector2传递给ScreenPointToRay(...),我强烈建议您使用Vector3, 该函数使用z与相机的距离,并推断z0f距离很难推断Ray
  3. 尝试使用camera.pixelWidthpixelHeight,因为Screen.widthheight坐标可能不一定存在 在相机的视野内。
  4. 所以我想尝试一下:

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

答案 1 :(得分:0)

我建议您检查console log是否有错误。如果你是“steptwo”,那么你不能成为'stepthree'的唯一方法就是出现错误。

我尝试了你的代码,并通过分配我自己的相机,我能够进入'stepthree'所以我必须假设它没有得到你的相机和一个空引用错误阻止你的脚本走得那么远。

Debug.Log是你的朋友。尝试插入Debug.Log(exterminatorCamera)以查看是否正在分配摄像机。您还可以使用Debug.DrawRay查看ScreenPointToRay函数输出的光线。