2022-11-15 22:58:54 +00:00
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 )
2022-11-15 22:58:54 +00:00
{
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 )
{
2023-02-09 11:53:30 +00:00
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 ) ;
}
2022-11-18 01:50:45 +00:00
}
2022-11-15 22:58:54 +00:00
itemGameObject = null ;
// TODO: Most likely, implement drop here.
2022-12-11 03:03:30 +00:00
go . transform . SetParent ( null , true ) ;
2022-11-15 22:58:54 +00:00
go . transform . localRotation = Quaternion . Euler ( oldRotation ) ;
}
}
}