Search Unity

Strange Interact Destination

Discussion in 'Navigation' started by Fluffeeker, Feb 11, 2017.

  1. Fluffeeker

    Fluffeeker

    Joined:
    Feb 6, 2017
    Posts:
    3
    Hi guys, I have problem that im trying to work out for few hours without solution. Here is a gif and explaination of the problem:

    https://gyazo.com/9713a81defc84b249a09974d69a2bafb

    The problem is: im right clicking (interaction-move) each of those 3 blocks. The thing is character is always running to one specific point to interact, no matter what. I would love to make it that character is running to closest point of the object, not that strange point. Its super annoying If I have big wall and character run around it to the middle on the other side...

    My related code:

    Code (CSharp):
    1.  
    2. public NavMeshAgent playerAgent;
    3. public void MoveToInteraction(NavMeshAgent playerAgent)
    4.     {
    5.         this.playerAgent = playerAgent;
    6.         playerAgent.stoppingDistance = 3f;
    7.         playerAgent.destination = this.transform.position;
    8.     }
    9.  
    10.     NavMeshAgent playerAgent;
    11.     public GameObject objectToSpawn;
    12.  
    13.     void Start()
    14.     {
    15.         playerAgent = GetComponent<NavMeshAgent>();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         if (Input.GetMouseButtonDown (1) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
    21.             GetInteraction ();
    22.         bool isRunning = false;
    23.         if (playerAgent.remainingDistance - playerAgent.stoppingDistance >= 0.1)
    24.             isRunning = true;
    25.         else
    26.             isRunning = false;
    27.         GetComponent<Animator>().SetBool("IsRunning", isRunning);
    28.     }
    29.  
    30.     void GetInteraction()
    31.     {
    32.         Ray interactionRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    33.         RaycastHit interactionInfo;
    34.         if (Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity))
    35.         {
    36.             GameObject interactedObject = interactionInfo.collider.gameObject;
    37.             if (interactedObject.tag == "Interactable Object")
    38.             {
    39.                 interactedObject.GetComponent<Interactable>().MoveToInteraction(playerAgent);
    40.             }
    41.             else if (interactedObject.tag == "Ground")
    42.             {
    43.                 playerAgent.stoppingDistance = 0;
    44.                 playerAgent.destination = interactionInfo.point;
    45.                 Instantiate(objectToSpawn, interactionInfo.point + new Vector3(0, 0.1f), Quaternion.Euler(90, 0, 0)); // click animation on the ground
    46.             }
    47.             else
    48.             {
    49.                 playerAgent.stoppingDistance = 0;
    50.                 playerAgent.destination = interactionInfo.point;
    51.             }
    52.         }
    53.     }
    Rescue me pls. Help in advance!
     
  2. Mycroft

    Mycroft

    Joined:
    Aug 29, 2012
    Posts:
    160
    Is your MoveToInteraction pathing to the closest location on the NavMesh from the center of the object?

    If so; internally it's finding the same point each time because it will always be the closest point to the center.

    It kinda sounds like you have this issue.
     
  3. Fluffeeker

    Fluffeeker

    Joined:
    Feb 6, 2017
    Posts:
    3
    Yea I think it's the same issue. I can't even make doors as interactable objects because they need to have collider's and my character is trying to open them always from the same side.