[英]How to rotate player object via script in Unity
I have a game on Unity3d and I want to rotate player object via script: 我在Unity3d上有一个游戏,我想通过脚本旋转播放器对象:
Camera.main.transform.Rotate(0, 10, 0);
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
: 而且,只有在标准脚本
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
}
}
}
But when I comment it, my mouse don't react, when I move it. 但是当我评论它时,当我移动它时鼠标没有反应。 How can I solve it?
我该如何解决?
Simply add a public variable to manage an additionnal angle you can change when you want, and tweek a little the LookRotation function : 只需添加一个公共变量来管理您可以在需要时更改的附加角度,然后teek一点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
}
}
And in the code used to rotate the camera : 在用于旋转相机的代码中:
MouseLook mouseLook = gameObjectHoldingMouseLook.GetComponent<MouseLook>() ;
mouseLook.myAngle += 10 ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.