Search Unity

Question Interact with near object while the player is looking at it

Discussion in 'Physics' started by Jakuiz, Oct 31, 2023.

  1. Jakuiz

    Jakuiz

    Joined:
    Aug 11, 2021
    Posts:
    4
    Hi everyone. I have it mostly done, except for this one bug.

    I have a boolean called, isInteracting which starts obviously as false.

    If I'm inside a certain range from the target object, I begin shooting rays to colliders looking for that object's tag, if I look at it, isInteracting equals true.
    The issue I have is that if I keep looking at it and leave the range, the isInteracting does not come back to false.

    By now, I know it's because the player is always in contact other collider (the ground) and because of that, I cannot set isInteracting to false when the player is not in the range of the target collider, because its always happening!

    Any help is really appreciated

    Player interact class code:
    Code (CSharp):
    1. public bool isInteracting;
    2.    
    3.     // Update is called once per frame
    4.     void FixedUpdate()
    5.     {
    6.         // Check for nearby colliders looking for the parchment.
    7.  
    8.         Collider[] colliderArray = Physics.OverlapSphere(transform.position, interactRange);
    9.         foreach (Collider collider in  colliderArray)
    10.         {
    11.             Debug.Log(collider.tag);
    12.             if (collider.tag == "parchment")
    13.             {
    14.                 // Shoot a ray when in range to make sure the player is looking at it.
    15.  
    16.                 Debug.Log("1");
    17.                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    18.  
    19.                 if (Physics.Raycast(ray, out RaycastHit hit))
    20.                 {
    21.                     if (hit.collider.tag == "parchment")
    22.                     {
    23.                         Debug.Log("2");
    24.                         isInteracting = true;
    25.                         Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow, 1.0f);
    26.                     }
    27.                     else
    28.                     {
    29.                         Debug.Log("3n");
    30.                         isInteracting = false;
    31.                     }
    32.                 }      
    33.             }
    34.            
    35.         }
    36.     }
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,425
    The only part of your code that sets isInteracting to false is inside the foreach, AND inside the first if condition, AND inside the second if condition. It would seem you want to set isInteracting false whenever ANY of these conditions fail.

    Instead of lines 27~31, just insert
    isInteracting = false;
    above line 6. This will make it assume that interaction is false on every update, UNLESS all of these other conditions happen to succeed.
     
  3. Jakuiz

    Jakuiz

    Joined:
    Aug 11, 2021
    Posts:
    4
    Oh, clearly a logical error. That was it, thank you for your help!