我在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
}
}
}
但是当我评论它时,我移动它时,我的鼠标没有反应。我该如何解决?
答案 0 :(得分: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 ;