projectEli/Assets/Inventory System/Scripts/Inventory/TargetAttachment.cs

62 lines
1.9 KiB
C#
Raw Normal View History

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)
{
2022-11-18 01:50:45 +00:00
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)
{
2022-11-18 01:50:45 +00:00
if (hideGameObject)
{
itemGameObject.SetActive(true);
}
itemGameObject = null;
// TODO: Most likely, implement drop here.
go.transform.SetParent(null, true);
go.transform.localRotation = Quaternion.Euler(oldRotation);
}
}
}