[img]https:// i.sstatic.net/yrrIaQE0.png[/img]

Есть 3 скрипта
Скрипт плеера
Код: Выделить всё
private void Update()
{
InteractionRay();
}
void InteractionRay()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
bool hitSomething = false;
if (Physics.Raycast(ray, out hit, interactionRange))
{
Iinteractable interactable = hit.collider.GetComponent();
if (interactable != null)
{
hitSomething = true;
interactionText.text = interactable.GetDescription();
if (Input.GetKeyDown(KeyCode.N))
{
interactable.Interact();
}
}
}
interactionUI.SetActive(hitSomething);
//Debug.DrawRay(ray.origin,interactionRange* ray.direction, Color.green);
Debug.DrawLine(ray.origin, hit.point, Color.red);
}
Код: Выделить всё
public interface Iinteractable
{
void Interact();
string GetDescription();
}
Код: Выделить всё
public class Test : MonoBehaviour, Iinteractable
{
public string GetDescription()
{
return "Press";
}
public void Interact()
{
Debug.Log("Did Hit");
}
}
Я хотел бы отметить, что я очень новичок в программировании, и код по сути представляет собой копию учебника с небольшими изменениями для моего случая. Скрипты были правильно назначены персонажу и объекту для взаимодействия.
Подробнее здесь: https://stackoverflow.com/questions/792 ... -collision