简单的2D场景,我在X轴上向左/向右移动,在Y轴上跳跃/下落。无论如何,我默认将武器设置为正面。我让它成为玩家的孩子,但是当我使用左箭头键或A键时,武器不会切换到两边。不过,这并不是真正令人惊讶的事情。
我对如何实现这种转变感到困惑。
public GameObject gun; // define the gun object然后在函数内部
getComponent<GameObject>(); //Which I may not need to do ?在我的移动脚本中:
if(GetKeyDown(KeyCode.A) || GetKeyDown(KeyCode.LeftArrow)){
gun.transform.position = player.position.rotate; // ???
}出于某些原因,我想不到如何简单地反转lol为什么是我?
发布于 2018-08-28 23:03:44
您需要一个新的变量isFacingRight来跟踪现有的facing。正如其他人所提到的,您可以翻转局部缩放来改变方向。
bool isFacingRight = true;
void Update(){
//other stuff
if(GetKeyDown(KeyCode.A) || GetKeyDown(KeyCode.LeftArrow)){
FaceLeft();
}
if(GetKeyDown(KeyCode.D) || GetKeyDown(KeyCode.RightArrow)){
FaceRight();
}
}
void FaceRight(){
if(!isFacingRight){
gun.transform.localScale = new Vector3(1, 1, 1);
isFacingRight = true;
}
}
void FaceLeft(){
if(isFacingRight){
gun.transform.localScale = new Vector3(-1, 1, 1);
isFacingRight = false;
}
}*已解决*
我复制了我的武器,把它翻到了左边。我手动点击了左面武器上的停用复选框。下面是我使用的代码:
public GameObject gun = Pshoot.gun; // Right facing gun
public GameObject gun1 = Pshoot.gun1; // Left facing gun
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) {
GetComponent<Renderer>().material = rgtFace;
gun.SetActive(false);
gun1.SetActive(true);
// isFacingRight = false;
// FaceLeft();
// gun.transform.localScale = new Vector3(-1,1,-1);
}在这个左移动检查中,我将右枪设置为假,并激活左枪使其出现。(忽略注释掉的部分,我今天学习了localeScale,所以我把它保留下来供以后参考。你可能不需要它,所以忽略它)
if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
{
GetComponent<Renderer>().material = lftFace;
gun.SetActive(true);
gun1.SetActive(false);
// isFacingRight = true;
}上面是设置右面喷枪的代码。现在只是提醒一下,你不需要材料代码。这只是我为了让我的精灵动起来而做的。你会注意到上面写着lftFace,但是我不小心把正确的脸部图像放到了左边的变量中,lol,它不会影响游戏,我可以接受它。不要评判我!:p
无论如何,感谢所有评论帮助我的人!非常喜欢网络程序员fam!
https://stackoverflow.com/questions/52057639
复制相似问题