Vector3团结一致,似乎无法获得玩家位置



我该如何获得玩家的位置,这样我就可以将物品扔到玩家周围大约5个单位的地方,这样他们扔下物品后就不会及时捡起物品,我该如何获取玩家的Vector3位置?我在很多方面都试过了,但仍然无法使其发挥作用,我正在这里尝试:

public void DropItem()
{
if(slotsItem)
{
slotsItem.transform.parent = null;
slotsItem.gameObject.SetActive(true);
slotsItem.transform.position = Vector3.lastpos;
}

那么这是完整的代码以及

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Slot : MonoBehaviour
{
public Item slotsItem;
public Transform player;
Sprite defaultSprite;
Text amountText;
public void CustomStart()
{
defaultSprite = GetComponent<Image>().sprite;
amountText = transform.GetChild(0).GetComponent<Text>();
this.player = GameObject.FindWithTag("Player").transform;
Vector3 lastpos = player.position;
}
public void DropItem()
{
if(slotsItem)
{
slotsItem.transform.parent = null;
slotsItem.gameObject.SetActive(true);
slotsItem.transform.position = Vector3.lastpos;
}
}
public void CheckForItem()
{
if(transform.childCount > 1)
{
slotsItem = transform.GetChild(1).GetComponent<Item>();
GetComponent<Image>().sprite = slotsItem.itemSprite;
if(slotsItem.amountInStack > 1)
amountText.text = slotsItem.amountInStack.ToString();
}
else
{
slotsItem = null;
GetComponent<Image>().sprite = defaultSprite;
amountText.text = "";
}
}
}

Vector3.lastpos应该是什么?

我想你更想在类中存储一个字段

private Vector3 lastpos;

在中分配该字段

public void CustomStart()
{
...
lastpos = player.position;
}

然后像一样使用

public void DropItem()
{
...
slotsItem.transform.position = lastpos;
...
}

尽管问题是为什么不简单地使用当前位置

slotsItem.transform.position = player.position;

最新更新