0

I have a game on Unity3d and I want to rotate player object via script:

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

enter image description here

I can't rotate player object, so I try to rotate it's child object with camera component. And I can do it only when comment 2 strings in the standart script MouseLook.cs:

[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
        }
    }      
}

But when I comment it, my mouse don't react, when I move it. How can I solve it?

1 Answer 1

1

Simply add a public variable to manage an additionnal angle you can change when you want, and tweek a little the LookRotation function :

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

And in the code used to rotate the camera :

MouseLook mouseLook = gameObjectHoldingMouseLook.GetComponent<MouseLook>() ;
mouseLook.myAngle += 10 ;
Sign up to request clarification or add additional context in comments.

5 Comments

But I can't attach this script to any object in the scene, because it must be derived from MonoBehaviour. And if I will do it, I got error: Object reference not set to an instance of an object. UnityStandardAssets....FirstPersonController.cs:57. I can't acces variable myAngle
@dima Just wondering...why do you have this class separate from a Gameobject? I'm curious about the structure of your code.
@Serlite What do you mean? MouseLook.cs is a standard script and you don't need to attach it to any object. I don't attach and it works
@dima It's perfectly fine to have non-Monobehaviour classes, I'm just curious about how you're approaching structuring your code, since that could influence the appropriate solution for your situation. (My initial thoughts were that this script could be adapted just fine to work as a component on an object, but you know your project best.)
If MouseLook script is not attached to any gameobject, it is at least, referenced by another script (FirstPersonController according to you), thus, supposing the MouseLook instance is public in the FirstPersonController, you can do : gameObjectFPController.GetComponent<FirstPersonController>().MouseLook.myAngle += 10 ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.