Reputation: 2809
I have the following code,
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;
}
}
However in my pickup
function, my GetKeyDown
call never happens?
Why would this be? (Booleans never change that I am using to watch this with).
As a note: stepone
becomes true, but none of the other steps do.
EDIT:
I made another step, it appears it makes it to steptwo
but no further...
EDIT: It appears the ScreenPointToRay
isn't doing anything...?
Upvotes: 0
Views: 2029
Reputation: 8173
A few things i can point out:
Vector3
, and you only give it an x
and a y
Vector2
into ScreenPointToRay(...)
, i highly recommend using a Vector3
, as
the function uses z
for distance from camera, and inferring a z
of 0f
distance makes it pretty hard to infer your Ray
camera.pixelWidth
and pixelHeight
, as Screen.width
and height
coordinates may not necessarily exist
within the camera's view.So heres what i would try:
int distance = 100f;
int x = exterminatorCamera.pixelWidth / 2;
int y = exterminatorCamera.pixelHeight / 2;
Ray ray = exterminatorCamera.ScreenPointToRay(new Vector3(x, y, distance));
Upvotes: 1
Reputation: 1268
I recommend checking your console log for errors. If you are making it to 'steptwo', the only way you would not make it to 'stepthree' is if there was an error.
I tried your code and by assigning my own Camera I was able to get to 'stepthree' so I have to assume it isn't getting your camera and a null reference error is stopping your script from getting that far.
Debug.Log is your friend. Try inserting a Debug.Log(exterminatorCamera) to see if the camera is being assigned. You can also use Debug.DrawRay to see the ray being output by the ScreenPointToRay function.
Upvotes: 0