Я работаю над проектом, в котором персонаж игрока, сфера и цилиндр существуют на плоскости.List.Contains in Unity C# не работает
Способ, которым он должен работать, заключается в том, что персонаж обнаруживает сферу и может «поднять ее», посылая имя сферы в список и затем уничтожая объект. В этом случае имя (Sphere
) проверяется на использование inventory.Contains("Sphere")
, когда символ взаимодействует с полем, и если ключ находится в инвентаре, он будет «разблокирован», однако независимо от того, находится ли сфера в инвентаре или нет, коробка заблокирована.
Вот код:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class RayCasting : MonoBehaviour
{
float rayCoeff;
RaycastHit hit;
List<string> inventory = new List<string>();
// Use this for initialization
void Start()
{
rayCoeff = 3f;
}
void Cast()
{
Debug.DrawRay (transform.position, Vector3.right * rayCoeff, Color.red);
Debug.DrawRay (transform.position, Vector3.right * -rayCoeff, Color.red);
Debug.DrawRay (transform.position, Vector3.forward * rayCoeff, Color.red);
Debug.DrawRay (transform.position, Vector3.forward * -rayCoeff, Color.red);
if (Physics.Raycast (transform.position, Vector3.forward, out hit, rayCoeff, 1<<8)) {
if(hit.collider.tag == "Interactable") {
//Debug.Log ("Front Scan with collider!, Found Thing.");
if(Input.GetKey(KeyCode.E)) {
string inventoryClaim = hit.collider.ToString();
inventory.Add(inventoryClaim);
Destroy(hit.collider.gameObject);
}
}
}
else if (Physics.Raycast (transform.position, new Vector3(0, 0, -1), out hit, rayCoeff, 1<<8)) {
if(hit.collider.tag == "Interactable") {
//Debug.Log ("Back Scan with collider!, Found Thing.");
if(Input.GetKey(KeyCode.E)) {
string inventoryClaim = hit.collider.ToString();
inventory.Add(inventoryClaim);
Destroy(hit.collider.gameObject);
}
}
}
else if (Physics.Raycast (transform.position, Vector3.right, out hit, rayCoeff, 1<<8)) {
if(hit.collider.tag == "Interactable") {
//Debug.Log ("Right Scan with collider!, Found Thing.");
if(Input.GetKey(KeyCode.E)) {
string inventoryClaim = hit.collider.ToString();
inventory.Add(inventoryClaim);
Destroy(hit.collider.gameObject);
}
}
}
else if (Physics.Raycast (transform.position, new Vector3(-1, 0, 0), out hit, rayCoeff, 1<<8)) {
if(hit.collider.tag == "Interactable") {
//Debug.Log ("Left Scan with collider!, Found Thing.");
if(Input.GetKey(KeyCode.E)) {
string inventoryClaim = hit.collider.ToString();
inventory.Add(inventoryClaim);
Destroy(hit.collider.gameObject);
}
}
else if (hit.collider.tag == "Cylinder"){
if(Input.GetKey(KeyCode.E)) {
if(inventory.Contains("Sphere")) {
Debug.Log("Hooray!");
}
}
}
}
}
// Update is called once per frame
void Update() {
Cast();
}
}
Там нет ошибки, просто отсутствие признания.
Пожалуйста, прочтите описание тега перед его добавлением. –
Что происходит, когда вы сами проходите через код. Не могли бы вы рассказать нам о том, что проблема происходит в ..? что или где это определено «hit.collider» также работает над переформатированием фрагмента кода – MethodMan
Это потому, что 'hit.collider.ToString()' на самом деле не равен «Сфере», – Vlad