首先,我创建了过度效果并确保它有效,但是当我添加 Input.getmousebutton 时,它会干扰悬停,并且以前无论我将鼠标移到板上,悬停的方块都会更改其颜色,但现在它不再显示
RaycastHit info;
Ray ray = currentCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out info, 150, LayerMask.GetMask("Square","Hover","Highlight")))
{
//Get the indexes of the Square you hit
Vector2Int hitPosition = LookUpSquareIndex(info.transform.gameObject);
// From not hovering to hovering
if (currentHover == -Vector2Int.one)
{
currentHover = hitPosition;
Squares[hitPosition.x, hitPosition.y].layer = LayerMask.NameToLayer("Hover");
}
// From hovering one square to hovering another square
if (currentHover != hitPosition)
{
Squares[currentHover.x, currentHover.y].layer = containsValidMove(ref availableMoves, currentHover) ? LayerMask.NameToLayer("Highlight") : LayerMask.NameToLayer("Square");
currentHover = hitPosition;
Squares[hitPosition.x, hitPosition.y].layer = LayerMask.NameToLayer("Hover");
}
//pressing the mouse button
if (Input.GetMouseButtonDown(0))
{
if (chessPieces[hitPosition.x, hitPosition.y] != null)
{
//Is it Our Turn
if ((chessPieces[hitPosition.x, hitPosition.y].team==0 && isWhiteTurn) || (chessPieces[hitPosition.x, hitPosition.y].team == 1 && !isWhiteTurn))
{
currentlyDragging = chessPieces[hitPosition.x, hitPosition.y];
//getting a list of available Moves , heighlight the available moves
availableMoves = currentlyDragging.GetAvailableMoves(ref chessPieces, SQUARE_COUNT_X, SQUARE_COUNT_Y);
//Getting a Special Moves List
specialMove = currentlyDragging.GetSpecialMoves(ref chessPieces, ref moveList, ref availableMoves);
highlightSquares();
}
}
}
// Releasing the mouse Button
if (currentlyDragging != null && Input.GetMouseButtonUp(0))
{
Vector2Int previousPosition = new Vector2Int(currentlyDragging.currentX, currentlyDragging.currentY);
bool validMove = MoveTo(currentlyDragging, hitPosition.x, hitPosition.y);
if (!validMove)
currentlyDragging.setPosition(GetSquaresCenter(previousPosition.x, previousPosition.y));
currentlyDragging = null;
removehighlightSquares();
}
else
{
if (currentHover != -Vector2Int.one)
{
Squares[currentHover.x, currentHover.y].layer = (containsValidMove(ref availableMoves, currentHover)) ? LayerMask.NameToLayer("Highlight") : LayerMask.NameToLayer("Square");
currentHover = -Vector2Int.one;
if (currentlyDragging && Input.GetMouseButtonUp(0))
{
currentlyDragging.setPosition(GetSquaresCenter(currentlyDragging.currentX, currentlyDragging.currentY));
currentlyDragging = null;
removehighlightSquares();
}
}
}
// while dragging a piece
if (currentlyDragging)
{
Plane horizontalePlane = new Plane(Vector3.up , Vector3.up*Yoffset/1000);
float distance = 0.0f;
if(horizontalePlane.Raycast(ray, out distance))
currentlyDragging.setPosition(ray.GetPoint(distance)+Vector3.up*dragOffset);
}
}
我尝试添加多个功能,例如handleHoerEffect和HandleMouseButtonUp和Down
我认为问题可能出在此处的
else
。
// Releasing the mouse Button
if (currentlyDragging != null && Input.GetMouseButtonUp(0))
{
//your code
}
else
{
if (currentHover != -Vector2Int.one)
{
Squares[currentHover.x, currentHover.y].layer = (containsValidMove(ref availableMoves, currentHover)) ? LayerMask.NameToLayer("Highlight") : LayerMask.NameToLayer("Square");
currentHover = -Vector2Int.one;
//your code
}
}
我猜测这段代码是在 Update() 循环或类似的循环中运行的。第 1 帧以下条件将为真,并且 currentHover 将被分配一个值:
if (currentHover == -Vector2Int.one)
{
currentHover = hitPosition;
Squares[hitPosition.x, hitPosition.y].layer = LayerMask.NameToLayer("Hover");
}
下一帧,如果您没有释放鼠标按钮,则此条件将为 false:
(currentlyDragging != null && Input.GetMouseButtonUp(0))
会将代码路由到上面提到的 else 语句,并且 if (currentHover != -Vector2Int.one)
将为 true,因为它在前一帧中被分配了值
这最终将导致图层被分配给
LayerMask.NameToLayer("Square")
并且当前的悬停被设置为默认值,如果我的上述假设是正确的,这个循环将继续
Squares[currentHover.x, currentHover.y].layer = (containsValidMove(ref availableMoves, currentHover)) ? LayerMask.NameToLayer("Highlight") : LayerMask.NameToLayer("Square");
currentHover = -Vector2Int.one;
我无法在本地进行完全测试,因为所提供的代码片段仅显示了一小部分上下文,因此我最初的假设可能完全错误,但我希望这无论如何都会有所帮助