如何从Mouse Look Script访问其他类

时间:2016-05-16 13:19:16

标签: c# unity3d

你能帮助我从Mouse Look Script中访问其他类的变量,因为我似乎无法引用其他类(主要是我创建的类)。

任何帮助都将不胜感激。

    private Quaternion m_CharacterTargetRot;
    private Quaternion m_CameraTargetRot;
    private bool m_cursorIsLocked = true;
    private int LeftCounter ;
    private int RightCounter;

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


    //  Declaring focused rotations
        Vector3 CharToTar = target.position - character.position;
        Quaternion CharRotate = Quaternion.LookRotation (CharToTar);


        if (WitchRotation) {
            if (LeftCounter >= 50) {
                LeftCounter = 0;
                Debug.Log ("Witch Stare");

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

            }
            character.rotation = m_CharacterTargetRot;
            camera.localRotation = m_CameraTargetRot;

        } else {
            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;
                camera.localRotation = m_CameraTargetRot;
            }
        }
        // to detect rotation distance the camera rotated
        if(Input.GetAxis("Mouse X") < 0){
            LeftCounter++;          //Left rotation distance
            Debug.Log("Moved left " + LeftCounter + " times");
        }
         if(Input.GetAxis("Mouse X") > 0){
            RightCounter++;         //Right rotation distance
            Debug.Log("Mouse right " + RightCounter + " times");
            Debug.Log(RightCounter);

        }


        UpdateCursorLock();
    }

    public void SetCursorLock(bool value)
    {
        lockCursor = value;
        if(!lockCursor)
        {//we force unlock the cursor if the user disable the cursor locking helper
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
        }
    }

    public void UpdateCursorLock()
    {
        //if the user set "lockCursor" we check & properly lock the cursos
        if (lockCursor)
            InternalLockUpdate();
    }

    private void InternalLockUpdate()
    {
        if(Input.GetKeyUp(KeyCode.Escape))
        {
            m_cursorIsLocked = false;
        }
        else if(Input.GetMouseButtonUp(0))
        {
            m_cursorIsLocked = true;
        }

        if (m_cursorIsLocked)
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
        else if (!m_cursorIsLocked)
        {
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
        }
    }

    Quaternion ClampRotationAroundXAxis(Quaternion q)
    {
        q.x /= q.w;
        q.y /= q.w;
        q.z /= q.w;
        q.w = 1.0f;

        float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);

        angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);

        q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);

        return q;
    }

1 个答案:

答案 0 :(得分:1)

  

如何访问其他课程

也许您问的是如何从另一个类访问变量/函数。您要访问的变量或函数必须为df <- data_frame(a=1:10) mutate(df, a=a+5) mutate(df, "a"=a+5) #identical to first, not really a character variable mutate_(df, a=quote(a+5)) mutate_(df, .dots=list(a=quote(a+5))) mutate_(df, .dots=setNames(list(quote(a+5)),"a")) 而不是public

private

public class ScriptA : MonoBehaviour{ public int playerScore = 0; void Start() { } public void doSomething() { } } 中的playerScore访问变量ScriptA

ScriptB