For days I am struggling to implement the highlighting functionality in game as there is not much tutorial also for this. I did how ever make the cell of the grid change the material color but it looks ugly that's why I prefer to choose this method (see pic).

How can I implement a highlighting method that will work the same on link.


enter image description here


My try:

 private bool first_sel = false;
    private void Update() {
        if (Input.GetMouseButton(0)) {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out pressed)) {
                if (pressed.transform.tag == "cell") {
                    if (!first_sel) {
                        GameObject sel = Instantiate(sel_cell, new Vector3(pressed.transform.position.x,
                            pressed.transform.position.y, pressed.transform.position.z - 0.55f), Quaternion.identity);
                    }


                }
            }
        }

enter image description here

  • This looks like something you could do with a 9-slice sprite appropriately sized & rotated. Have you tried this approach? Where did you run into trouble? – DMGregory Dec 3 at 3:04
  • No. But I did manage to make something like that I used gameobject but I have problem making the highlight stretch. – Shulz Dec 4 at 4:08
  • Can you update your question to detail this problem? – DMGregory Dec 4 at 4:16
  • @DMGregory Done. – Shulz Dec 4 at 7:03

I would just use the line render with a transparent material and a slight offset on the Z axis so it doesn't clip with your board.

public float DistanceFromBoard;

private bool mouseDownOnLastFrame = false;
private GameObject startingCell;
private GameObject hoveredCell;
private Vector3 mouseWorldPosition;
private LineRenderer lineRenderer;

private void Awake()
{
    lineRenderer = GetComponent();
    lineRenderer.enabled = false;
}

private void Update()
{
    if(Input.GetMouseButton(0))
    {
        HandleDrag();
    }
    else if(mouseDownOnLastFrame) 
    {
        FinishHighlight();
    }
}

private void HandleDrag()
{
    RaycastHit hit;
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

    mouseDownOnLastFrame = true;

    if (Physics.Raycast (ray, out hit)) 
    {
        currentCell = hit.collider.gameObject;
        mouseWorldPosition = hit.point;

        if (startingCell != null && go.tag == "cell") // instead of using a tag here I would add the cells to a layer and use a layermask
        {
            startingCell = go;
            lineRenderer.enabled = true;
        }
    }

    lineRenderer.positions[0] = startingCell.transform.position - new Vector3(0f, 0, DistanceFromBoard);
    lineRenderer.positions[1] = hoveredCell.transform.position - new Vector3(0f, 0, DistanceFromBoard);
}

private void FinishHighlight()
{
    startingCell = null;
    lineRenderer.enabled = false;

    // here you might want to validate the highighted word and save the word to a collection of found words.

    // You're also going to want to copy this line renderer's positons and copy them to a new gameobject.
    // this is because we cannot only use one line to highlight all the words
}

Also remember to add a line renderer component to your gameobject and give it 2 blank positions.

As for the material, create a new material using the standard shader but change it from "opaque" to "transparent" and lower the alpha.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.