projectEli/Assets/Inventory System/Scripts/Inventory/TargetAttachment.cs
2023-02-09 03:53:30 -08:00

69 lines
2.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SimpleInventorySystem
{
public class TargetAttachment : MonoBehaviour
{
public ItemTags itemTag;
private GameObject itemGameObject;
public Vector3 position;
public Vector3 rotation;
public Vector3 scale = Vector3.one;
public bool hideGameObject;
public bool KeepOldRotationOnDrop;
private Vector3 oldRotation = new Vector3();
public bool AttachItemGameObject(GameObject go, ItemTags targetItemTag, PickUp pickUp)
{
if (itemTag == targetItemTag || itemTag == ItemTags.Any)
{
itemGameObject = go;
if (hideGameObject)
{
itemGameObject.SetActive(false);
return true;
}
oldRotation = go.transform.localRotation.eulerAngles;
go.transform.SetParent(transform, false);
if(pickUp == null)
{
go.transform.localPosition = position;
go.transform.localRotation = Quaternion.Euler(rotation);
go.transform.localScale = scale;
}
else
{
pickUp.AlterItemGameObjectTransform();
}
// TODO: possibly handle transform rotations, scales, etc.
return true;
}
return false;
}
public void DetachItemGameObject(GameObject go)
{
if (hideGameObject)
{
if (itemGameObject == null)
{
Debug.LogError($"TargetAttachment: Item Game Object is null, please correctly set up your Item with a PickUp. Game Object name: {itemGameObject.name}");
}
else
{
itemGameObject.SetActive(true);
}
}
itemGameObject = null;
// TODO: Most likely, implement drop here.
go.transform.SetParent(null, true);
go.transform.localRotation = Quaternion.Euler(oldRotation);
}
}
}