Need help to grab 1 object at a time from multiple objects available
Hello, I am trying to do a minigame where you need to steal objects from NPC's. I am using the new Unity input system and currently testing with a PS4 controller. You basically have 3 buttons you can use to steal for example: Square, LT and RT. (You hold to grab and release to let go). My Goal is to Instantiate the Object grabbed to the corresponding position, according to the Input.
The boxes are items you can grab the 1st picture is what i would like to do. I already have the controller part working. I just cant seem to get only 1 item at a time..
Square = C Position | LT = D Position | RT = E Position
However when i Press any of these buttons all objects go to the input position, to the point you can have unlimited Objects in one same position. I only want 1 object to be in each position. If the position is occupied then you cant grab any more objects unless you drop the item in hand.
I was thinking in highlighting the nearest object to the player and add only that one (if (itemHighlighted == true) { add obj to location }, if(locationD == full) { cant grab object })... However i cant highlight only 1 item... like in the picture below...
Here is the script i have for now, please help! I would really appreciate it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SelectionManagerScript : MonoBehaviour
{
[SerializeField] private Material Highlight;
[SerializeField] private Material NonStealableItemMaterial;
[SerializeField] private Material StealableItemMaterial;
GameObject ClosestItem;
public bool NeutralItem;
public bool canSteal;
//TO DO:
//Highlight only the closest item to the player
//Grab only 1 item at a time
//Place item grabbed according to input location
public GameObject FindClosestItem()
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Item");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
Debug.Log(closest);
distance = curDistance;
Debug.Log(distance);
}
}
return closest;
}
private void OnTriggerEnter(Collider other)
{
canSteal = true;
ClosestItem = FindClosestItem();
ShadeMaterial(other);
}
/*private void OnTriggerStay(Collider other)
{
canSteal = true;
ClosestItem = FindClosestItem();
ShadeMaterial(other);
}*/
private void OnTriggerExit(Collider other)
{
canSteal = false;
ClosestItem = FindClosestItem();
DefaultMaterial(other);
}
void ShadeMaterial(Collider item)
{
var selectionRenderer = ClosestItem.GetComponent<Renderer>();
if (selectionRenderer != null)
{
selectionRenderer.material = Highlight; //highlight the obj
}
}
void DefaultMaterial(Collider item)
{
bool stealObj;
stealObj = item.GetComponent<ItemCollider>().StealableItem;
if (stealObj == true)
{
var selectionRenderer = ClosestItem.GetComponent<Renderer>();
if (selectionRenderer != null)
{
selectionRenderer.material = StealableItemMaterial;
}
}
if (stealObj == false)
{
var selectionRenderer = ClosestItem.GetComponent<Renderer>();
if (selectionRenderer != null)
{
selectionRenderer.material = NonStealableItemMaterial;
}
}
}
}