与对象交互并将其添加到库存中时,它添加了最后一个创建的GameObject
因此,游戏知道它直接击中的对象,直到在可相互作用的脚本中调用互动()函数为止。我已经使用调试来尝试找到问题的位置,并且似乎是该功能,或者是在该脚本中。
我想使游戏读取它点击的游戏对象,然后将其分配给该对象的“项目”添加到库存中并删除对象,并且可以做到这一点,除非它专门添加了最后创建的GameObject。如果我在GameObject中交换“项目”,它仍将开始从上一个创建的对象添加和删除对象。
如果可以提供帮助,我已经附加了所有涉及的脚本,但是根据我可以看出这是可相互作用的脚本的问题。
我最好的猜测是以某种方式使可相互作用的脚本认为这不是最后创建的对象,但是我找不到有关如何做到这一点的信息。
的问题不是太多
对这个问题很抱歉,有大量的代码,我希望对Mouselook
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouseLook : MonoBehaviour
{
public bool hasInteracted = false;
public bool wasFpressed = false;
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
public float radius = 4f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
if (Input.GetKeyDown(KeyCode.F))
{
wasFpressed = true;
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, radius))
{
Interactable interactable = hit.collider.GetComponent<Interactable>();
Debug.Log(interactable);
if (interactable != null)
{
hasInteracted = true;
}
}
else
{
Debug.Log("Nothing");
}
}
}
}
:互动:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
public class Interactable : MonoBehaviour
{
public LayerMask interactableLayerMask;
public Item item;
public mouseLook inter;
public virtual void Interact()
{
//at this point the console tells me the gameObject is the last created object
Debug.Log(gameObject);
}
void Update()
{
if (inter.hasInteracted && inter.wasFpressed)
{
inter.hasInteracted = false;
inter.wasFpressed = false;
Interact();
}
}
}
itempickup:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemPickup : Interactable
{
public override void Interact()
{
base.Interact();
Debug.Log("Interacted with " + item.name);
PickUp();
}
void PickUp()
{
Debug.Log("Picking up " + item.name);
bool wasPickedUp = Inventory.instance.Add(item);
if (wasPickedUp)
{
Destroy(gameObject);
Debug.Log("Destroyed Item");
}
}
}
intectory: nectory:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
#region Singleton
public static Inventory instance;
void Awake()
{
if (instance != null)
{
Debug.LogWarning("More than one instance of Inventory found!");
return;
}
instance = this;
}
#endregion
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallback;
public int space = 20;
public List<Item> items = new List<Item>();
public bool Add (Item item)
{
if (!item.isDefaultItem)
{
if (items.Count >= space)
{
Debug.Log("Note enough space in inventory");
return false;
}
else
{
items.Add(item);
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
}
return true;
}
public void Remove(Item item)
{
items.Remove(item);
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
}
item:item:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
new public string name = "New Item";
public Sprite icon = null;
public bool isDefaultItem = false;
}
So the game knows what object it hit right up until the Interact() function is called in the Interactable script. I've used Debugs to try and find where the issue is and it seems to be that function, or rather in that script.
I want to make the game read which gameObject it hit and then put the "Item" it has assigned to that object to then add to the Inventory and delete the object, and it does that, except it adds the last created gameObject specifically. If I swap the "Items" in the gameObjects it will still start adding and deleting objects from the last created one.
I've attached all the scripts that are involved in this if that can help, but from what I can tell it's a problem with the Interactable script.
My best guess would be to somehow make the Interactable script think it's not the last created object, but I couldn't find info on how I could do that.
I'm sorry for this question with a huge amount of code, I hope it's not too much for a question
mouseLook:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouseLook : MonoBehaviour
{
public bool hasInteracted = false;
public bool wasFpressed = false;
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
public float radius = 4f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
if (Input.GetKeyDown(KeyCode.F))
{
wasFpressed = true;
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, radius))
{
Interactable interactable = hit.collider.GetComponent<Interactable>();
Debug.Log(interactable);
if (interactable != null)
{
hasInteracted = true;
}
}
else
{
Debug.Log("Nothing");
}
}
}
}
Interactable:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
public class Interactable : MonoBehaviour
{
public LayerMask interactableLayerMask;
public Item item;
public mouseLook inter;
public virtual void Interact()
{
//at this point the console tells me the gameObject is the last created object
Debug.Log(gameObject);
}
void Update()
{
if (inter.hasInteracted && inter.wasFpressed)
{
inter.hasInteracted = false;
inter.wasFpressed = false;
Interact();
}
}
}
ItemPickup:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemPickup : Interactable
{
public override void Interact()
{
base.Interact();
Debug.Log("Interacted with " + item.name);
PickUp();
}
void PickUp()
{
Debug.Log("Picking up " + item.name);
bool wasPickedUp = Inventory.instance.Add(item);
if (wasPickedUp)
{
Destroy(gameObject);
Debug.Log("Destroyed Item");
}
}
}
Inventory:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
#region Singleton
public static Inventory instance;
void Awake()
{
if (instance != null)
{
Debug.LogWarning("More than one instance of Inventory found!");
return;
}
instance = this;
}
#endregion
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallback;
public int space = 20;
public List<Item> items = new List<Item>();
public bool Add (Item item)
{
if (!item.isDefaultItem)
{
if (items.Count >= space)
{
Debug.Log("Note enough space in inventory");
return false;
}
else
{
items.Add(item);
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
}
return true;
}
public void Remove(Item item)
{
items.Remove(item);
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
}
Item:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
new public string name = "New Item";
public Sprite icon = null;
public bool isDefaultItem = false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论