2016-12-14 85 views
0

通過腳本旋轉選手客體我有Unity3d遊戲,我想通過腳本來旋轉選手對象:如何團結

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

enter image description here

我不能轉動選手對象,所以我嘗試使用相機組件旋轉它的子對象。我能做到這一點,只有當註釋2串在非標準腳本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 
     } 
    }  
} 

但是,當我評論它,我的鼠標還沒有反應過來,當我移動它。我該如何解決它?

回答

1

只需添加一個公共變量來管理,當你想你可以改變的其它附加的角度,和tweek一點的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 ; 
+0

但我無法將此腳本附加到場景中的任何對象,因爲它必須來自'MonoBehaviour'。如果我會這樣做,我得到錯誤:'對象引用未設置爲對象的實例。 UnityStandardAssets .... FirstPersonController.cs:57'。我無法訪問變量'myAngle' – dima

+0

@dima只是想知道......爲什麼你將這個類與GameObject分開?我很好奇你的代碼的結構。 – Serlite

+0

@Serlite你是什麼意思? 'MouseLook.cs'是一個標準腳本,你不需要將它附加到任何對象。我沒有附加,它的工作 – dima