C# 在Unity中拖动相机时如何防止鬼魂移动?
我有下面的脚本连接到我的主相机。这是一款虚拟现实应用程序。脚本允许我在按下鼠标按钮时旋转相机 脚本可以工作,但问题是,当我按下鼠标按钮时,视图会继续向左侧轻微旋转。当我松开鼠标按钮时,移动停止 我找不到这场“鬼”运动的原因C# 在Unity中拖动相机时如何防止鬼魂移动?,c#,unity3d,C#,Unity3d,我有下面的脚本连接到我的主相机。这是一款虚拟现实应用程序。脚本允许我在按下鼠标按钮时旋转相机 脚本可以工作,但问题是,当我按下鼠标按钮时,视图会继续向左侧轻微旋转。当我松开鼠标按钮时,移动停止 我找不到这场“鬼”运动的原因 公共类DragCamera:MonoBehavior { //标记以跟踪是否正在拖动 bool isDragging=false; //摄像机运动的起点 浮动startMouseX; 浮式StartMouse; //摄像机组件 摄像机; //用于初始化 void Start(
公共类DragCamera:MonoBehavior
{
//标记以跟踪是否正在拖动
bool isDragging=false;
//摄像机运动的起点
浮动startMouseX;
浮式StartMouse;
//摄像机组件
摄像机;
//用于初始化
void Start()
{
//拿我们的相机组件
cam=GetComponent();
Cursor.visible=false;
}
//每帧调用一次更新
无效更新()
{
//如果我们按下左键,还没有开始拖动
if(Input.GetMouseButtonDown(0)&&!isDraging)
{
//将标志设置为true
IsDraging=true;
//保存鼠标的起始位置
startMouseX=Input.mousePosition.x;
startMouseY=Input.mousePosition.y;
}
//如果我们没有按左边的btn,我们正在拖动
else if(Input.GetMouseButtonUp(0)和&isDraging)
{
//将标志设置为false
IsDraging=错误;
}
}
void LateUpdate()
{
//检查我们是否正在拖动
if(ISDRAGING)
{
//计算当前鼠标位置
float-endMouseX=Input.mousePosition.x;
float endMouseY=Input.mousePosition.y;
//差异(屏幕坐标)
float diffX=endMouseX-startMouseX;
float diffY=endMouseY-startMouseY;
//屏幕的新中心
float newCenterX=屏幕宽度/2+diffX;
float newCenterY=屏幕高度/2+差异;
//获取世界坐标,这是我们要看的地方
Vector3 LookHerePoint=cam.ScreenToWorldPoint(新Vector3(newCenterX、newCenterY、cam.nearClipPlane));
//让我们的相机看“注视点”
transform.LookAt(LookHerePoint);
//下一次呼叫的起始位置
startMouseX=endMouseX;
startMouseY=endMouseY;
}
}
}
float newCenterX=Screen.width/2+diffX;
float newCenterY=屏幕高度/2+差异;
你可能想考虑使用数学中的括号,包括乘法/除法和加减法。这有助于防止编译器解释
float newCenterX=Screen.width/2+diffX代码>作为
float newCenterX=Screen.width/(2+diffX)代码>
而不是预期的
float newCenterX=(Screen.width/2)+diffX代码>
在使用mono编译器(unity)时,我个人发现它实际上需要整数除法。即12f/2
,并将其舍入到最接近的整数。尝试用相同的基元类型(如12f/2f
明确划分可能是因为将屏幕转换为世界空间坐标时存在舍入或精度问题,您是否检查过鼠标固定时的diffX/Y值?调试到屏幕可能会有所帮助。如果是这样的话,你可以忽略在一定公差范围内的值。这就解决了它。非常感谢:)
public class DragCamera : MonoBehaviour
{
// flag to keep track whether we are dragging or not
bool isDragging = false;
// starting point of a camera movement
float startMouseX;
float startMouseY;
// Camera component
Camera cam;
// Use this for initialization
void Start()
{
// Get our camera component
cam = GetComponent<Camera>();
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
// if we press the left button and we haven't started dragging
if (Input.GetMouseButtonDown(0) && !isDragging)
{
// set the flag to true
isDragging = true;
// save the mouse starting position
startMouseX = Input.mousePosition.x;
startMouseY = Input.mousePosition.y;
}
// if we are not pressing the left btn, and we were dragging
else if (Input.GetMouseButtonUp(0) && isDragging)
{
// set the flag to false
isDragging = false;
}
}
void LateUpdate()
{
// Check if we are dragging
if (isDragging)
{
//Calculate current mouse position
float endMouseX = Input.mousePosition.x;
float endMouseY = Input.mousePosition.y;
//Difference (in screen coordinates)
float diffX = endMouseX - startMouseX;
float diffY = endMouseY - startMouseY;
//New center of the screen
float newCenterX = Screen.width / 2 + diffX;
float newCenterY = Screen.height / 2 + diffY;
//Get the world coordinate , this is where we want to look at
Vector3 LookHerePoint = cam.ScreenToWorldPoint(new Vector3(newCenterX, newCenterY, cam.nearClipPlane));
//Make our camera look at the "LookHerePoint"
transform.LookAt(LookHerePoint);
//starting position for the next call
startMouseX = endMouseX;
startMouseY = endMouseY;
}
}
}