Когда я делаю это с дверью, она работает, но когда я в ключе, она не работает; Я не понимаю почему (я тщательно искал, но так и не нашел решения). Кроме того, когда я пытаюсь взаимодействовать с ключом, он всегда показывает мне debug.log("none"), который я вставил в свой луч-скрипт.
PS: я положил интерфейс моего ray-скрипта к другому скрипту, чтобы было более понятно, но это все равно ничего не меняет, если вы скажете мне, что я закомментировал интерфейс
ray script:
Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// interface IInteractable
// {
// public void Interact();
// }
public class Interactor : MonoBehaviour
{
[SerializeField] private Transform InteractorSource;
[SerializeField] private float interactRange = 5f;
// public bool active = false;
// Update is called once per frame
void Update()
{
Ray r = new Ray(InteractorSource.position, InteractorSource.forward);
// Debug.DrawRay(r.origin, r.direction * 10);
if (Input.GetKeyDown(KeyCode.E))
{
// active = Physics.Raycast(r, out RaycastHit hitInfo, interactRange);
// out hitInfo
if (Physics.Raycast(r, out RaycastHit hitInfo, interactRange))
{
if (hitInfo.collider.gameObject.TryGetComponent(out IInteractable interactObj))
{
Debug.Log("interacted");
interactObj.Interact();
}else Debug.Log("none");
}
}
}
}
Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Key : MonoBehaviour, IInteractable
{
// [SerializeField] private GameObject door;
public void Interact()
{
Debug.Log("key");
// gameObject.SetActive(false);
// door.GetComponent().SetHasKey(true);
}
}
Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorScript : MonoBehaviour, IInteractable
{
private bool haskey = false;
public void Interact()
{
// Debug.Log("hello that's a door");
if (haskey)
{
gameObject.transform.GetComponent().SetTrigger("Activate");
}
}
public bool GetHasKey()
{
return haskey;
}
public void SetHasKey(bool newHasKey)
{
haskey = newHasKey;
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... -key-but-i