我试过按上面的箭头让我的小立方体变成高立方体,但我想不通:(
如果可以,请帮助我。
在我的游戏中,这样做很重要,否则我的想法将行不通。
我试过:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FormShifting : MonoBehaviour {
public GameObject myObject1;
public GameObject myObject2;
int high;
void Update () {
if (Input.GetKey("up") && high == 0)
{
myObject1.SetActive(false);
myObject2.SetActive(true);
}
if (Input.GetKey("down") && high == 1)
{
myObject2.SetActive(false);
myObject1.SetActive(true);
}
}
}
最佳答案
你需要使用 Input.GetKeyDown
来知道什么时候一个键被按下,Input.GetKey
只有当一个按钮被按下时才为 true
一直按着。
此外,如果可能,不要使用字符串参数,而是使用 KeyCode
枚举。
最后,您需要更改 high
的值,否则第二个 if 永远不会是 true
。
using UnityEngine;
public class FormShifting : MonoBehaviour {
public GameObject myObject1;
public GameObject myObject2;
int high;
void Update () {
if (Input.GetKeyDown(KeyCode.UpArrow) && high == 0)
{
myObject1.SetActive(false);
myObject2.SetActive(true);
high = 1;
}
if (Input.GetKeyDown(KeyCode.DownArrow) && high == 1)
{
myObject2.SetActive(false);
myObject1.SetActive(true);
high = 0;
}
}
}
关于c# - 将图 A 更改为图 B,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48790910/