Help with pick up script
Hi,
I'm working on a pick up script but I can't figure out how to solve it unfortunately. Right now I have a working script where you look at a certain object and a text appears on screen depending on which tag it has, see script below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class LookAt : MonoBehaviour
{
public TMP_Text hitTextSource;
private float hitDistance = 3.0f;
[SerializeField]
private List<textGroup> textGroups = new List<textGroup>();
[System.Serializable] public class textGroup
{
public string hitTag;
public string hitText;
}
void Update()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, hitDistance))
{
textGroup hitTextGroup = GetHitTextGroup(hit.collider.gameObject);
if(hitTextGroup != null)
{
hitTextSource.text = hitTextGroup.hitText;
hitTextSource.enabled = true;
}
else
{
hitTextSource.enabled = false;
}
}
else
{
hitTextSource.enabled = false;
}
if (hitDistance < 3.0f)
{
hitTextSource.enabled = false;
}
}
textGroup GetHitTextGroup(GameObject gameObject)
{
foreach(textGroup group in textGroups)
{
if(gameObject.CompareTag(group.hitTag))
return group;
}
return null;
}
}
I would like to have a script which you place on all the objects that needs to be picked up by the player and also check which tag it has so you can have one for batteries, notes etc.
The optimal solution would be if you're in range (hitDistance) and the text appears on screen, for example "Pick up battery", then you can pick up the correct object. Somehow I need to solve the tagging part, if I understand it correctly, I can't access hitTag from another script because it's inside of a class. I've tried using OnTriggerEnter but with the script above the distance will be all wrong.
Any ideas how to solve this? Please note that the script above will be placed on a "Manager" object and not on the objects that will be picked up