枪不会在同一个地方产卵

时间:2018-05-04 18:03:38

标签: c# unity3d

我制作了一个枪系统,我需要拿起我产生的枪支 但问题是当我拿起枪时它不会在我想要的地方产生,因为玩家轮换这是一张图片:https://netane54544-gmail.tinytake.com/media/75f9e3?filename=1525456534383_04-05-2018-08-53-27.png&sub_type=thumbnail_preview&type=attachment&width=1198&height=654

这里还有另一张枪的图片,只是为了展示我的目标:https://netane54544-gmail.tinytake.com/media/75f9f5?filename=1525456736315_04-05-2018-08-56-51.png&sub_type=thumbnail_preview&type=attachment&width=1198&height=654

以下是我正在使用的代码:

foreach (Gun item in gunList)
    {
        if (item.gunType == "Normal" && guns_inInventory[playerScript.keyPress] == false)
        {
            playerGuns[playerScript.keyPress] = Instantiate(Weapon_Normal, Camera.main.transform.position + Normalview.offSetPosition, Quaternion.identity) as GameObject;
            playerGuns[playerScript.keyPress].name = gunName;
            playerGuns[playerScript.keyPress].transform.parent = gameObject.transform;
            playerGuns[playerScript.keyPress].transform.eulerAngles = Normalview.startOffsetRotation;
            setGunActive(playerScript.keyPress);

            //Store gundata in inventory
            item.Named = false;
            Inventory[playerScript.keyPress] = item;
            guns_inInventory[playerScript.keyPress] = true;
        }
        else if (item.gunType == "Stride" && guns_inInventory[playerScript.keyPress] == false)
        {
            playerGuns[playerScript.keyPress] = Instantiate(Weapon_Stride, Camera.main.transform.position + Normalview.offSetPosition, Quaternion.identity) as GameObject;
            playerGuns[playerScript.keyPress].name = gunName;
            playerGuns[playerScript.keyPress].transform.parent = gameObject.transform;
            playerGuns[playerScript.keyPress].transform.eulerAngles = Strideview.startOffsetRotation;
            setGunActive(playerScript.keyPress);

            //Store gundata in inventory
            item.Named = false;
            Inventory[playerScript.keyPress] = item;
            guns_inInventory[playerScript.keyPress] = true;
        }
    }

我试图找到问题,但我被卡住了。

1 个答案:

答案 0 :(得分:3)

NormalView.offSetPosition仅在没有旋转时存储位移。

例如,假设您的offsetPosition是Vector(0,0,1):

Camera.main.transform.position + NormalView.offSetPosition将占据相机的位置并始终添加Vector(0,0,1),它在世界轴上有效地向前移动1个单位,有效地忽略了相机的旋转。

要解决此问题,请将实例化位置更改为:

Camera.main.transform.position + 
(Camera.main.transform.forward * NormalView.offSetPosition.z) +
(Camera.main.transform.right * NormalView.offSetPosition.x) +
(Camera.main.transform.up * NormalView.offSetPosition.y)

这通过获取受其旋转影响的相机的向前/向右/向上矢量来考虑相机的旋转。通过将它与您的偏移量(在该方向上的距离)相乘,无论相机的旋转如何,它都将始终在相对于您角色的相同位置产生。