繁体   English   中英

Unity 2D-武器不会切换到左侧

[英]Unity 2D - Weapon doesn't switch to left

简单的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; // ??? 
 }

出于某种原因,我想不出如何简单地逆转笑声?

您需要一个新变量isFacingRight来跟踪现有面。 正如其他人所提到的,您可以翻转局部比例尺来更改方向。

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);
    }

在此向左移动检查中,我将右枪设置为false并激活了左枪以显示。 (忽略注释掉的部分,我今天学会了localeScale,所以我将其保留以备后用。您可能不需要它,因此请忽略它)

if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
    {

        GetComponent<Renderer>().material = lftFace;
        gun.SetActive(true);
        gun1.SetActive(false);
       // isFacingRight = true;
    }

上面是设置右面喷枪的代码。 现在,请注意,您将不需要物料代码。 那只是我做的动画精灵。 您会注意到它上面显示了lftFace,但是我不小心将右脸图像放在leftface变量中,大声笑不影响游戏,我可以接受。 不要评价我! :p

无论如何,感谢所有评论帮助我的人! 多爱网络程序员成名!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM