I read that game objects need to have a tag to be returned. For example some game objects will be inventory items. I want to simulate picking or placing them in this simple way:
the game object placed in the world e.g a cube will have a tag InventoryItem
if the player is 1.0 range of the cube (or an object with tag InventoryItem) - if the cube is visible (as set in Inspector) - hide cube, else if invisible then show cube. It is fine to work for any objects 1.0 of player but usually there will be only one such object at a time. It is good to toggle the show or hide of an object when I press the āiā button. The whole thing needs to be done with press of the key.
So press i and the nearby (can be even 1.0 of the facing of player) the item that is near hides or unhides with the press of āiā
Never mind I solved it. Firstly for beginners, I guess Inspector is where you assign what will be player variable (you set in Inspector the variable fpViewer to be the placed player in the world), same for the object that will use the script.
public class InventoryItemTakePlace : MonoBehaviour
{
public Renderer itemRenderer;
public GameObject inventoryItem;
public GameObject fpViewer;
float inventoryItemDistance;
void Start()
{
itemRenderer = GetComponent<Renderer>();
itemRenderer.enabled = true;
}
// Update is called once per frame
void Update()
{
inventoryItemDistance = Vector3.Distance(inventoryItem.transform.position, fpViewer.transform.position);
if((Input.GetKey("i")) && inventoryItemDistance <= 3.5f)
{
itemRenderer.enabled = false;
}
if((Input.GetKey("o")) && inventoryItemDistance <= 3.5f)
{
itemRenderer.enabled = true;
}
}
}