NodeManager.GetClosestNode C# (CSharp) Method

GetClosestNode() public method

Gets the tile from location.
public GetClosestNode ( Vector3 location ) : Node
location Vector3
return Node
    public Node GetClosestNode(Vector3 location)
    {
        int xIndex = (int)Mathf.Floor ((location.x - left.x) / objectManager.MapData.NodeSize.x);
        int zIndex = size_y + ((int)Mathf.Floor ((location.z - left.y) / objectManager.MapData.NodeSize.y));

        if(objectManager.MapData.IsIsoGrid)
        {
            xIndex = (int)Mathf.Round((2f*location.x + objectManager.MapData.NodeSize.x - 2f * left.x) / objectManager.MapData.NodeSize.x) - 2;
            zIndex = (int)Mathf.Round((location.z - (objectManager.MapData.NodeSize.y / 2f) - right.y - ((xIndex%2==1)?objectManager.MapData.NodeSize.y/2f:0f)) / objectManager.MapData.NodeSize.y);
        }

        if (xIndex >= size_x)
            xIndex = size_x - 1;
        else if (xIndex < 0)
            xIndex = 0;

        if (zIndex >= size_y)
            zIndex = size_y - 1;
        else if (zIndex < 0)
            zIndex = 0;

        return nodes [xIndex, zIndex];
    }

Usage Example

示例#1
0
    private NodeHit CalculateHit(NodeManager nodeManager)
    {
        Vector2 point = transform.position;
        NodeHit hit   = new NodeHit();

        hit.nodeManager = nodeManager;
        hit.node        = nodeManager.GetClosestNode(point);
        hit.normal      = hit.node.side == NodeSide.Left ?
                          new Vector2(hit.node.velocity.y, -hit.node.velocity.x).normalized :
                          new Vector2(-hit.node.velocity.y, hit.node.velocity.x).normalized;
        return(hit);
    }