using UnityEngine;
public abstract class InteractableBase : MonoBehaviour, IInteractable
{
[SerializeField] private string interactionPrompt;
public string GetInteractionPrompt()
{
return interactionPrompt;
}
public virtual void Interact(GameObject interactor)
{
Debug.Log($"{interactor.name} interacted with {gameObject.name}");
}
}
using System;
using UnityEngine;
public class InteractionSystem : MonoBehaviour
{
[SerializeField] private float interactionRange = 2f;
[SerializeField] private LayerMask interactableLayer;
[SerializeField] private InputManager inputManager;
private void Awake()
{
if (inputManager == null)
{
inputManager = GetComponent<InputManager>();
}
}
public bool isInteract;
private InteractableBase _currentInteractable;
[SerializeField] private bool IsInteracting;
void Update()
{
CheckForInteractable();
HandleInteractionInput();
IsInteracting = Input.GetKeyDown(KeyCode.E);
}
private void CheckForInteractable()
{
Collider[] hits = Physics.OverlapSphere(transform.position, interactionRange, interactableLayer);
if (hits.Length > 0)
{
_currentInteractable = hits[0].GetComponent<InteractableBase>();
if (_currentInteractable != null)
{
Debug.Log(_currentInteractable.GetInteractionPrompt());
}
}
else
{
_currentInteractable = null;
}
}
private void HandleInteractionInput()
{
if (_currentInteractable != null && inputManager.IsInteracting && !isInteract)
{
isInteract = true;
_currentInteractable.Interact(gameObject);
isInteract = false;
}
}
private void OnDrawGizmos()
{
// Dessiner la sphère d'interaction
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, interactionRange);
}
}
using UnityEngine;
public class PickableItem : InteractableBase
{
[SerializeField] private Item item;
public override void Interact(GameObject interactor)
{
base.Interact(interactor); // Optionnel : affiche un message de débogage générique
AddItemToInventory(interactor);
Destroy(gameObject); // Supprimer l'objet de la scène après ramassage
}
private void AddItemToInventory(GameObject interactor)
{
// Logique pour ajouter l'objet à un inventaire (à adapter selon votre système)
var inventory = interactor.GetComponent<Inventory>();
if (inventory != null)
{
inventory.AddItem(item);
Debug.Log($"Added {item} to inventory.");
}
else
{
Debug.LogWarning("No Inventory component found on interactor!");
}
}
}
using UnityEngine;
public interface IInteractable
{
void Interact(GameObject interactor);
string GetInteractionPrompt();
}
j’ai beau essaayer appuyer le button interagir pour enfin de ramaser l’item mais rien ne se passe je ne comprend pas ou j’ai faut et pourtant j’ai bien configurer