The question is answered, right answer was accepted
Display GameObjects tag using Raycast
Okay so I have been trying to add the ability to pick up weapons for the ground(or shelf), and add them to my player object. I was able to produce this using the box collider and calculating the distance from each other. However, I now need to use RayCast to pickup the object, as the old method would pickup any object it was near, not just one specific item.
The problem that im facing is when the RayCast 'fires', it is able to find the GameObjects name, but it is unable to find it tag. I have tried various answers on this forums, from hit.collider.tag
to hit.transform.tag
but neither have worked.
Here is my code:
//Raycast Variables
private RaycastHit hit;
private bool RayHit;
private float Range = 2.0f;
void Awake ()
{
playerScript = GetComponent<GunController>();
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.E))
{
Debug.Log("Key E pressed.");
if (Physics.Raycast (transform.position, transform.forward, out hit, Range))
{
Debug.Log("Ray Hit");
if (hit.collider.tag.Equals("Weapon"))
{
//Debug.Log("Picking up" + item.name);
//Destroy(gameObject);
//PickUpItem();
Debug.Log("Hit");
}
}
}
}
I do not receive any errors, yet the only to Debug.Log lines are Debug.Log("Key E pressed.");
and Debug.Log("Ray Hit");
. I do not receive the Debug.Log("Hit");
. Any help would be appreciated, and any documentation regarding RayCast that I could use in the future would also help. Thank you.
Answer by Jackio · Mar 02, 2018 at 12:18 PM
UPDATE : I was able to fix the problem, by moving the script to the players camera, instead of the item itself, which actually makes sense now that i think about it. I also had to work my way around a few more errors that decided to sprout up! But it is now working!