Search Unity

NavMeshAgent and moving on moving objects

Discussion in 'Navigation' started by StonedLover, May 31, 2017.

  1. StonedLover

    StonedLover

    Joined:
    Apr 16, 2014
    Posts:
    44
    Hey,
    I got the following issue:
    I use the NavMeshSurface to generate the surface on the object. Then I setup a simple AI movment via clicks to make him move. If the platform is idle he can move but as soon it moves the unit is shifting to the edge of the moving platform and staying there. Here is the Character Script which I use very simple:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. [RequireComponent(typeof(NavMeshAgent))]
    6. public class Character : MonoBehaviour {
    7.  
    8.     private bool unitSelected = true;
    9.  
    10.     private float health;
    11.  
    12.     private Vector3 Destination;
    13.     private Task task;
    14.     private Inventory inventory;
    15.  
    16.     private NavMeshAgent agent;
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.         agent = GetComponent<NavMeshAgent>();
    21.      
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if (unitSelected)
    28.         {
    29.            // Debug.Log("isSelected");
    30.             if (Input.GetKey(KeyCode.Mouse1))
    31.             {
    32.                 //Debug.Log("PressedKey");
    33.                 RaycastHit hit;
    34.                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    35.                 if (Physics.Raycast(ray, out hit, 100))
    36.                 {
    37.                     if (hit.transform.tag.Equals("Ground"))
    38.                     {
    39.                         //Debug.Log("HitGround at " + hit.transform.position);
    40.                         makeMove(hit.point);
    41.                     }
    42.                 }
    43.             }
    44.         }
    45.     }
    46.  
    47.  
    48.     void makeMove(Vector3 desti)
    49.     {
    50.         Debug.Log("MakeMove to : " + desti);
    51.         //agent.destination = destination;
    52.         agent.SetDestination(desti);
    53.      
    54.     }
    55. }
    56.  
    To move the platform its just one stmt in the update method:
    Code (CSharp):
    1.         this.transform.position = Vector3.MoveTowards(this.transform.position, pos.transform.position, Time.deltaTime);
    2.  


    Any Ideas where this issue came from?
     
  2. StonedLover

    StonedLover

    Joined:
    Apr 16, 2014
    Posts:
    44
    Okay.. for some reason it now works... I setup the things again and hurray it does waht it should.
    But I run into the next issue since the platform is moving the unit go back to the old world position and dont stay at the local grid of the moving platform.

    have I to reupdate the position to the navmeshagent while its moving all the time or is there a work around?
     
  3. Jakob_Unity

    Jakob_Unity

    Unity Technologies

    Joined:
    Dec 25, 2011
    Posts:
    269
    You're describing a use-case that is not supported yet.
    The internal representation of the navmesh cannot doesn't recognize the move. It's treated as if you remove the navmesh and add a new navmesh right next to the previous one.

    To reliably move a navmesh you need a Move method (similar to the Move method on rigidbodies) - but it's not supported yet.

    (it's also mentioned in the FAQ here : https://github.com/Unity-Technologies/NavMeshComponents#faq )
     
  4. Codinablack

    Codinablack

    Joined:
    May 8, 2016
    Posts:
    12
    There is nothing wrong with this script. It must be on the generated surface of the platform or somewhere else, this script reads perfectly fine, in fact you can use default ethan with that script and a navemesh and a plane and he moves just fine.

    As far as navmeshagent.move, it definitely does exist... wtf?
     
  5. niflying

    niflying

    Joined:
    Jul 11, 2012
    Posts:
    46
    Set tthe Character's transform.parent = platform.transform ?

    It work's for me, when I put a character on a moving boat.
     
  6. RendCycle

    RendCycle

    Joined:
    Apr 24, 2016
    Posts:
    329
    I'm also wondering how to do this. According to this video (8:59, Unite Europe 2017) it says it is possible to move an Object with a NavMeshAgent that is, let's say, walking on a moving platform in realtime. But it will be resource intensive.

    @niflying, can you provide me more details on how you successfully did this with a moving Boat?