【问题标题】:Unity C# How to turn item in hands when player turnsUnity C#如何在玩家转动时转动手中的物品
【发布时间】:2013-12-01 05:22:12
【问题描述】:

我对统一很陌生,但我制作了一个基本的 FPS 游戏,当我拿着枪时,我想制作它,这样当你的玩家转动时,手中的物品会旋转以显示转动。例如,在玩使命召唤时,旋转角色时枪也会旋转。这是我的代码,但它不起作用

    void Update(){
    this.rotateEquppedOnTurn();
}
private void rotateEquppedOnTurn(){
    if(this.equippedItem != null){
        InteractEquppableItem equip = this.equippedItem.gameObject.GetComponent<Interaction>() as InteractEquppableItem;
        if(equip.rotatesWhenTurn){
            float rotX = Input.GetAxis("Mouse X");
            float rotY = Input.GetAxis("Mouse Y");
            Quaternion tempRot = new Quaternion();
            Quaternion tempCam = GameObject.Find("PlayerCamera").transform.rotation;
            tempRot.x = tempCam.x + rotX;
            tempRot.y = tempCam.y + rotY;
            tempRot.z = tempCam.z;
            this.equippedItem.gameObject.transform.rotation = tempRot;
        }
    }
}

当使用此代码转动角色时,枪只是以一种奇怪的方式旋转,这与我对旋转脚本的预期不太一样

【问题讨论】:

    标签: c# rotation unity3d quaternions


    【解决方案1】:
    1. Quaternions 不是 vectors
    2. 我建议您首先观看 Unity 网站上的 vector tutorial
    3. 本教程的最后一点介绍了 cross products 是什么以及为什么要使用它们 - 具体来说,您可以使用它们来获得一个相对轴,您可能希望围绕该轴旋转某些东西。

    【讨论】:

      【解决方案2】:

      不要像这样直接指定旋转。

      this.equippedItem.gameObject.transform.rotation = tempRot;
      

      而不是使用类似的东西

      this.equippedItem.gameObject.transform.Rotate(new Vector3(x,y,z));

      您可以使用鼠标移动导出 x,y,z

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-05
        相关资源
        最近更新 更多