用于物品拾取选择的 C# Unity 脚本
C# Unity script for item pick up choice
我目前有一个脚本可以将 2 个预制件实例化为游戏对象。其中附加了一个脚本,将 game-object
设置为 false,然后将其添加到我的 inventory
数组中。这是另一个脚本,但我的问题是另一个未被选中的游戏对象仍然存在。应该是你有2个选择。一旦你拿了一个,另一个就消失了。我不知道该怎么做,谁能帮忙。
public int moralityCounter=0;
public bool Inventory;
public void Store()
{
if (gameObject.CompareTag("Good"))
{
moralityCounter++;
Debug.Log(moralityCounter);
gameObject.SetActive(false);
}
if (gameObject.CompareTag("Bad"))
{
moralityCounter--;
Debug.Log(moralityCounter);
gameObject.SetActive(false);
}
}
如果只有单个 good
和单个 bad
标记的对象,您可以将标记为好和坏的所有对象设置为不活动。
private void DeactivateAllGoodBads()
{
// Deactivate all goods
GameObject[] goodObjects = GameObject.FindGameObjectsWithTag("Good");
foreach (GameObject goodObject in goodObjects)
{
goodObject.SetActive(false);
}
// Deactivate all bads
GameObject[] badObjects = GameObject.FindGameObjectsWithTag("Bad");
foreach (GameObject badObject in badObjects)
{
badObject.SetActive(false);
}
}
public void Store()
{
bool isGood = gameObject.CompareTag("Good");
bool isBad = gameObject.CompareTag("Bad");
if (isGood)
{
moralityCounter++;
Debug.Log(moralityCounter);
}
if (isBad)
{
moralityCounter--;
Debug.Log(moralityCounter);
}
if (isGood || isBad)
{
DeactivateAllGoodBads();
}
}
如果有多个,你可以做一些事情,比如只禁用那些离存储对象较近的对象。
private void DeactivateCloseGoodBads(Vector3 position, float maxDistance)
{
// Deactivate close goods
GameObject[] goodObjects = GameObject.FindGameObjectsWithTag("Good");
foreach (GameObject goodObject in goodObjects)
{
// Check distance of found good
if (Vector3.Distance(position, goodObject.transform.position) <= maxDistance) {
goodObject.SetActive(false);
}
}
// Deactivate close bads
GameObject[] badObjects = GameObject.FindGameObjectsWithTag("Bad");
foreach (GameObject badObject in badObjects)
{
// Check distance of found bad
if (Vector3.Distance(position, badObject.transform.position) <= maxDistance) {
badObject.SetActive(false);
}
}
}
public void Store()
{
bool isGood = gameObject.CompareTag("Good");
bool isBad = gameObject.CompareTag("Bad");
if (isGood)
{
moralityCounter++;
Debug.Log(moralityCounter);
}
if (isBad)
{
moralityCounter--;
Debug.Log(moralityCounter);
}
if (isGood || isBad)
{
DeactivateCloseGoodBads(gameObject.transform.position, 10f);
}
}
我目前有一个脚本可以将 2 个预制件实例化为游戏对象。其中附加了一个脚本,将 game-object
设置为 false,然后将其添加到我的 inventory
数组中。这是另一个脚本,但我的问题是另一个未被选中的游戏对象仍然存在。应该是你有2个选择。一旦你拿了一个,另一个就消失了。我不知道该怎么做,谁能帮忙。
public int moralityCounter=0;
public bool Inventory;
public void Store()
{
if (gameObject.CompareTag("Good"))
{
moralityCounter++;
Debug.Log(moralityCounter);
gameObject.SetActive(false);
}
if (gameObject.CompareTag("Bad"))
{
moralityCounter--;
Debug.Log(moralityCounter);
gameObject.SetActive(false);
}
}
如果只有单个 good
和单个 bad
标记的对象,您可以将标记为好和坏的所有对象设置为不活动。
private void DeactivateAllGoodBads()
{
// Deactivate all goods
GameObject[] goodObjects = GameObject.FindGameObjectsWithTag("Good");
foreach (GameObject goodObject in goodObjects)
{
goodObject.SetActive(false);
}
// Deactivate all bads
GameObject[] badObjects = GameObject.FindGameObjectsWithTag("Bad");
foreach (GameObject badObject in badObjects)
{
badObject.SetActive(false);
}
}
public void Store()
{
bool isGood = gameObject.CompareTag("Good");
bool isBad = gameObject.CompareTag("Bad");
if (isGood)
{
moralityCounter++;
Debug.Log(moralityCounter);
}
if (isBad)
{
moralityCounter--;
Debug.Log(moralityCounter);
}
if (isGood || isBad)
{
DeactivateAllGoodBads();
}
}
如果有多个,你可以做一些事情,比如只禁用那些离存储对象较近的对象。
private void DeactivateCloseGoodBads(Vector3 position, float maxDistance)
{
// Deactivate close goods
GameObject[] goodObjects = GameObject.FindGameObjectsWithTag("Good");
foreach (GameObject goodObject in goodObjects)
{
// Check distance of found good
if (Vector3.Distance(position, goodObject.transform.position) <= maxDistance) {
goodObject.SetActive(false);
}
}
// Deactivate close bads
GameObject[] badObjects = GameObject.FindGameObjectsWithTag("Bad");
foreach (GameObject badObject in badObjects)
{
// Check distance of found bad
if (Vector3.Distance(position, badObject.transform.position) <= maxDistance) {
badObject.SetActive(false);
}
}
}
public void Store()
{
bool isGood = gameObject.CompareTag("Good");
bool isBad = gameObject.CompareTag("Bad");
if (isGood)
{
moralityCounter++;
Debug.Log(moralityCounter);
}
if (isBad)
{
moralityCounter--;
Debug.Log(moralityCounter);
}
if (isGood || isBad)
{
DeactivateCloseGoodBads(gameObject.transform.position, 10f);
}
}