简体   繁体   English

cursor 和鼠标 position alignment

[英]cursor and mouse position alignment

I am trying to highlight tiles, that are hovered by the mouse.我正在尝试突出显示鼠标悬停的图块。 This is my code:这是我的代码:

void Update ()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    RaycastHit hit;
    if (Physics.Raycast(ray, out hit))
    {
        Vector3 point = hit.point;
        Vector2Int gridPoint = Geometry.GridFromPoint(point);

        tileHighlight.SetActive(true);
        tileHighlight.transform.position = Geometry.PointFromGrid(gridPoint);
        if (Input.GetMouseButtonDown(0))
        {
          // ... 

Edit:编辑:

public class Geometry
{
    static public Vector3 PointFromGrid(Vector2Int gridPoint)
    {
        float x = -3.5f + 1.0f * gridPoint.x;
        float z = -3.5f + 1.0f * gridPoint.y;
        return new Vector3(x, 0, z);
    }

    static public Vector2Int GridPoint(int col, int row)
    {
        return new Vector2Int(col, row);
    }

    static public Vector2Int GridFromPoint(Vector3 point)
    {
        int col = Mathf.FloorToInt(4.0f + point.x);
        int row = Mathf.FloorToInt(4.0f + point.z);
        return new Vector2Int(col, row);
    }
}

The cursor object is not under the mouse how can I fix and align it? cursor object不在鼠标下怎么固定和对齐?

I am trying to highlight tiles, that are hovered by the mouse.我正在尝试突出显示鼠标悬停的图块。 This is my code:这是我的代码:

void Update ()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    RaycastHit hit;
    if (Physics.Raycast(ray, out hit))
    {
        Vector3 point = hit.point;
        Vector2Int gridPoint = Geometry.GridFromPoint(point);

        tileHighlight.SetActive(true);
        tileHighlight.transform.position = Geometry.PointFromGrid(gridPoint);
        if (Input.GetMouseButtonDown(0))
        {
          // ... 

Edit:编辑:

public class Geometry
{
    static public Vector3 PointFromGrid(Vector2Int gridPoint)
    {
        float x = -3.5f + 1.0f * gridPoint.x;
        float z = -3.5f + 1.0f * gridPoint.y;
        return new Vector3(x, 0, z);
    }

    static public Vector2Int GridPoint(int col, int row)
    {
        return new Vector2Int(col, row);
    }

    static public Vector2Int GridFromPoint(Vector3 point)
    {
        int col = Mathf.FloorToInt(4.0f + point.x);
        int row = Mathf.FloorToInt(4.0f + point.z);
        return new Vector2Int(col, row);
    }
}

The cursor object is not under the mouse how can I fix and align it? cursor object不在鼠标下怎么固定和对齐?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM