введите здесь описание изображения
введите здесь описание изображения
Существует 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