I’m designing a 3D third person game in which the player can interact with object when they walk up to them (within the collider) and press a button. I’m currently using physics raycast to detect interactable objects, but if I get too close or touch the object, it no longer detects it, making it very unreliable. How can I make it work 100% of the time.
Input controls, uses input system to call:
private void onInteract(InputAction.CallbackContext context)
{
if (active)
{
RaycastHit hit;
var ray = new Ray(this.transform.position, this.transform.position);
if (Physics.Raycast(ray, out hit, 100))
{
Interactable interaction = hit.collider.GetComponent<Interactable>();
if (interaction != null)
{
interaction.Interact();
}
}
}
}
The interaction function being called, can be overwritten by other scripts:
public virtual void Interact()
{
Debug.Log("Interact");
}