【问题标题】:How to rotate player object via script in Unity如何通过 Unity 中的脚本旋转播放器对象
【发布时间】:2016-12-15 01:34:16
【问题描述】:

我在 Unity3d 上有一个游戏,我想通过脚本旋转玩家对象:

Camera.main.transform.Rotate(0, 10, 0);

我无法旋转玩家对象,所以我尝试使用相机组件旋转它的子对象。而且我只能在标准脚本MouseLook.cs中注释2个字符串时才能做到这一点:

[Serializable]
public class MouseLook
{
    // Variables ..

    private Quaternion m_CharacterTargetRot;
    private Quaternion m_CameraTargetRot;

    public void Init(Transform character, Transform camera)
    {
        m_CharacterTargetRot = character.localRotation;
        m_CameraTargetRot = camera.localRotation;
    }

    public void LookRotation(Transform character, Transform camera)
    {
        float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
        float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;

        m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
        m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);

        if(clampVerticalRotation)
            m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);

        if(smooth)
        {
            character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
                smoothTime * Time.deltaTime);
            camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
                smoothTime * Time.deltaTime);
        }
        else
        {
            //character.localRotation = m_CharacterTargetRot; // move y axe
            //camera.localRotation = m_CameraTargetRot;       // move x axe
        }
    }      
}

但是当我评论它时,当我移动它时,我的鼠标没有反应。我该如何解决?

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    只需添加一个公共变量来管理您可以在需要时更改的附加角度,并使用 LookRotation 函数:

     public float myAngle = 0 ;
    
    public void LookRotation(Transform character, Transform camera)
    {
        float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
        float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;
    
        // Add your angle here
        m_CharacterTargetRot *= Quaternion.Euler (0f, yRot + myAngle, 0f);
        m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
    
        if(clampVerticalRotation)
            m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);
    
        if(smooth)
        {
            character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
                smoothTime * Time.deltaTime);
            camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
                smoothTime * Time.deltaTime);
        }
        else
        {
            character.localRotation = m_CharacterTargetRot; // move y axe
            camera.localRotation = m_CameraTargetRot;       // move x axe
        }
    }
    

    在用于旋转相机的代码中:

    MouseLook mouseLook = gameObjectHoldingMouseLook.GetComponent<MouseLook>() ;
    mouseLook.myAngle += 10 ;
    

    【讨论】:

    • 但我不能将此脚本附加到场景中的任何对象,因为它必须派生自MonoBehaviour。如果我这样做,我会得到错误:Object reference not set to an instance of an object. UnityStandardAssets....FirstPersonController.cs:57。我无法访问变量myAngle
    • @dima 只是想知道......为什么你有这个类与游戏对象分开?我很好奇你的代码结构。
    • @Serlite 你是什么意思? MouseLook.cs 是标准脚本,您无需将其附加到任何对象。我不附加,它有效
    • @dima 拥有非 Monobehaviour 类是非常好的,我只是好奇你如何处理你的代码结构,因为这可能会影响适合你情况的解决方案。 (我最初的想法是这个脚本可以很好地适应作为对象上的一个组件,但你最了解你的项目。)
    • 如果MouseLook 脚​​本没有附加到任何游戏对象,它至少被另一个脚本引用(FirstPersonController 根据你),因此,假设 MouseLook 实例在 @987654329 中是公共的@,你可以这样做:gameObjectFPController.GetComponent&lt;FirstPersonController&gt;().MouseLook.myAngle += 10 ;
    猜你喜欢
    • 1970-01-01
    • 2022-12-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多