我有下面的代码,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