我有下面的代碼,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
沒有做任何事情...?
是'exterminatorCameraObject'空? – maksymiuk