Raycast not sensing object on ground? cant pick up, Help Please !!! :)
Problem: As part of an inventory script im working on i found something that has had me stuck for days and that is that the ray cast that senses the object is not doing so, when the object is sensed through an id check it is supposed to display a pick up text. The problem is that i have drawn the ray out and it appears 100% to be going strait through the collider but isn't sensing it. Now when i step back a few units it then works. I have all the colliders set up perfectly but im not sure with the code. I would appreciate any help, thanks ahead of time.
Please Note: The two scripts below communicate with each other, the item pick up script checks the id and the bool pickupable.
ItemPickUp Script:
public Inventory Inv;
public GameObject pickUpUI;
bool addedItem;
void Start ()
{
Inv = GameObject.Find ("Inventory Panel").GetComponent<Inventory> ();
pickUpUI.SetActive (false);
}
void Update ()
{
Debug.DrawRay (Camera.main.transform.position, Camera.main.transform.forward * 3, Color.green);
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 3, -1, QueryTriggerInteraction.Collide))
{
bool pickup = hit.transform.gameObject.GetComponent<Item3D> ().pickUpable;
if (pickup == true) {
Debug.Log ("Working 1");
int itemID = hit.transform.gameObject.GetComponent<Item3D> ().id;
pickUpUI.SetActive (true);
if (Input.GetKeyDown (KeyCode.E)) {
Debug.Log ("Working 2");
Inv.AddItem (itemID);
Destroy (hit.transform.gameObject);
}
} else {
pickUpUI.SetActive (false);
}
}
}
} Item3D Script:
public bool pickUpable = false;
public int id;
Inventory inv;
void Start()
{
//inv.items [id].itemID = id;
}
}
int pickup = hit.transform.gameObject.GetComponent<Item3D> ().pickUpable;
if (pickup == true) {
As first guess i think the problem lies there. You are assigning bool value to int and then testing the int value against a boolean true value, which i think should give error already.
Yeah sorry that was a quick add to the code outside of unity i forgot to change the int to a bool.
Okay then the code seems pretty good to me, maybe it would be good idea to check first if the hit.transform is actually an item that contains Item3D script?
Then its bit hard to guess whats going on, but what i have encountered something similar before was like i had my ray starting at middle of my character and it collided with the characters collider unless i was walking backwards. So it would be a good idea to check where the ray is colliding with hit.transform.name when it should be colliding with the pickup.
I will check out the hit.transform.name thanks
I checked it out and the player transform seems to be blocking it do you know how to allow the raycast through. @RiQQ92
You could change the rays starting position like
Camera.main.transform.position + Camera.main.transform.forward
but thats rather dirty way, so best way in my opinion would be to change the rays collision layers and give some specific layer to player like 'IgnoreRaycast'
Easy way to get correct layers for your ray is to set them in inspector, and for this you need to set:
public Layer$$anonymous$$ask myRayCollisions;
and then just in your code you can write it there like:
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 3, myRayCollisions.value, QueryTriggerInteraction.Collide))
{
Other way to set your collision layer is bit shifting but i wont go into that. Hope you get it working now, cheers.
Your answer