1
\$\begingroup\$

I have a pad I interact with in my game. How do I make a visual indicator like the one shown in the attached GIF? You see that there is a circle that appears on the tablet showing where the user is about to click.

I have a collider around the tab and on the index finger.

I have tried two methods that did not work for me. One of them is instantiating the game object I intend using as the indicator when the index finger's collider enters the collider around the tablet and then updating its local position to be that of the index finger and some offset. I then destroy it OnTriggerExit.

The indicator kind of move with the index finger but I could not get the exact behaviour I want. I'm also trying my best possible to avoid using Update()

Here is the method I tried that is closer to what I want:

public GameObject visualIndicator;
private float _moveSpeed = 15f;

private void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("indexFinger"))
    {
        reticle.SetActive(true);
    }
}

private void OnTriggerStay(Collider other)
{
    if (other.CompareTag("indexFinger"))
    {
        Vector3 targetPosition = other.ClosestPoint(transform.position);
        visualIndicator.transform.position = Vector3.Lerp(visualIndicator.transform.position, targetPosition, Time.deltaTime * _moveSpeed);
    }
}

private void OnTriggerExit(Collider other)
{
    if (other.CompareTag("indexFinger"))
    {
        reticle.SetActive(false);
    }   
}

For this, I initially have the visual indicator set inactive on the pad. I also have a collider on the pad that protrudes out a bit such that once the finger collider collides with it, the visual indicator is set active and its position is updated to be the position of the point of collision in OnTriggerStay.

This looks very close to what I want but the visual indicator moves a bit up from the pad when looked from the side because its position is updated to be the point of collision of the pad's collider and the finger's. I want the visual indicator to only stay on the pad and follow the finger as shown in the following GIF:

enter image description here

\$\endgroup\$
3
  • 1
    \$\begingroup\$ "The indicator kind of move with the index finger but I could not get the exact behaviour I want." - Can you be more specific? How exactly did you accomplish that (editor screenshots / sourcecode), what behavior did you get (video?) and how does it differ from the behavior you want? If you provide this information, then we might be able to tell you what mistake you made or what you are missing to get the behavior you want. \$\endgroup\$
    – Philipp
    Commented Aug 18, 2023 at 6:58
  • \$\begingroup\$ @Philipp, I have updated my question by providing more information. \$\endgroup\$ Commented Aug 19, 2023 at 7:25
  • \$\begingroup\$ Is this a VR game? If so, please add the VR tag. \$\endgroup\$
    – Kevin
    Commented Aug 22, 2023 at 1:25

1 Answer 1

1
\$\begingroup\$

Right now, you have this code for OnTriggerStay:

private void OnTriggerStay(Collider other) {
    if (other.CompareTag("indexFinger")) {
        Vector3 targetPosition = other.ClosestPoint(transform.position);
        visualIndicator.transform.position = Vector3.Lerp(visualIndicator.transform.position, targetPosition, Time.deltaTime * _moveSpeed);
    }
}

This aligns the visual indicator to a point on the finger's collider. It sounds like what you want is to align the visual indicator to a point on the pad's collider.

//...
new private Collider collider;

private void Awake() {
    collider = GetComponent<Collider>();
}

private void OnTriggerStay(Collider other) {
    if (other.CompareTag("indexFinger")) {
        Vector3 targetPosition = collider.ClosestPoint(other.transform.position);
        var viTransform = visualIndicator.transform;
        float t = Time.deltaTime * _moveSpeed;
        viTransform.position = Vector3.Lerp(viTransform.position, targetPosition, t);
    }
}

//...
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .