我有一个问题-我说的是问题,更多的是极大的不便,这使它变得烦人。
我正在使用XNA Monogame制作3D游戏(一项痛苦的工作,团结起来会更容易,但出于某种原因,我不想被打扰,事实并非如此。)并且我的相机旨在设定玩家的鼠标移至窗口/视口的中心,然后获取与下一帧之间的鼠标移动变化,该变化将用于旋转相机...正常工作。但是,既然我已经将这个项目转移到了PC上,而不是我正在使用的笔记本电脑上,那么相机在移动时变得非常不稳定。我不太确定如何解决它,我一直在弄弄我将要发布的代码片段中的一些值,但是它不再那么好用了。如果这可能是一个问题,则使用3个屏幕设置了此PC?
if (moveVector != Vector3.Zero)
{
//Normalise the vector - to stop us moving faster diagonally
moveVector.Normalize();
//Add smooth and speed
moveVector *= deltaTime * cameraSpeed;
//Move camera
Move(moveVector);
}
//Handle mouse movement
float deltaX;
float deltaY;
if (currentMouseState != previousMouseState)
{
//Save mouse location
deltaX = currentMouseState.X - (Game.GraphicsDevice.PresentationParameters.BackBufferWidth / 2);
deltaY = currentMouseState.Y - (Game.GraphicsDevice.PresentationParameters.BackBufferHeight / 2);
//Determines the speed of the rotation 0.1 is pretty quick and looks just about right
mouseRotationBuffer.X -= 0.1f * deltaX * deltaTime;
mouseRotationBuffer.Y -= 0.1f * deltaY * deltaTime;
if (mouseRotationBuffer.Y < MathHelper.ToRadians(-75.0f))
{
mouseRotationBuffer.Y = mouseRotationBuffer.Y - (mouseRotationBuffer.Y - MathHelper.ToRadians(-75.0f));
}
if (mouseRotationBuffer.Y > MathHelper.ToRadians(75.0f))
{
mouseRotationBuffer.Y = mouseRotationBuffer.Y - (mouseRotationBuffer.Y - MathHelper.ToRadians(75.0f));
}
Rotation = new Vector3(-MathHelper.Clamp(mouseRotationBuffer.Y, MathHelper.ToRadians(-75.0f), MathHelper.ToRadians(75.0f)),
MathHelper.WrapAngle(mouseRotationBuffer.X), 0);
deltaX = 0;
deltaY = 0;
}
Mouse.SetPosition(Game.GraphicsDevice.Viewport.Width / 2, Game.GraphicsDevice.Viewport.Height / 2);
在任何人说之前,我也将其发布到gamedev,但我想我也会尝试在某人知道某事的情况下尝试此操作。
答案 0 :(得分:0)
您的代码段看起来不错,但是,当您希望摄像机平稳旋转时,可以调整以使值随着增加或减少到新的鼠标位置而平滑。
在鼠标翻译及其转换属性期间,Unity简化了操作,您要做的就是根据鼠标的移动速度来调整鼠标的最后位置和新位置的值。
只需一些sudeo代码即可演示相机的不稳定旋转。
// This is a potential Unity fix for solving camera translations in general
// for smooth movement
void Update() {
Vector3 msNewPoint = Mouse.Position;
Vector3 adjustedPoint = msOldPoint - msNewPoint;
Vector3 slerpedValue = Vector3.Slerp(msOldPoint, adjustedPoint, 0.2f);
gameObject.Transform.Rotation *= slerpedValue;
}
我在多个屏幕上偶然发现了同一个问题,当我将鼠标固定在游戏视口上时,这对于我来说是一个简单的解决方法。
// Again this is sudo code to demonstrate clamping the mouse to prevent multi
// monitor dpi (IF you are using spanned displays.
// Ie nVidia surround, or AMD Eyefinity.
private Rectangle oldClipRect;
private bool oldClipSet;
void clampMouse(Control control) {
Rectangle screenRect = control.RectangleToScreen(control);
// Initialize this only once to prevent the clip being altered
// when the size of the screen changes
if (!oldClipSet) {
oldClipRect = Cursor.Clip;
oldClipSet = true;
}
// If the control is focused bind the mouse to the game screen,
// otherwise release it.
if (control.Focused)
Cursor.Clip = screenRect;
else Cursor.Clip = oldClipRect;
}