Beispiel #1
0
    // monobehaviour ///////////////////////////////////////////////////////////
    void Awake()
    {
        // create projection
        var go = GameObject.CreatePrimitive(PrimitiveType.Cylinder);

        go.name = "NAVIGATION2D_AGENT";
        // project object to 3D (at y=0.5 so feet are at y=0 on navmesh)
        go.transform.position = NavMeshUtils2D.ProjectObjectTo3D(transform.position);
        agent       = go.AddComponent <NavMeshAgent>();
        rigidbody2D = GetComponent <Rigidbody2D>();
        collider2D  = GetComponent <Collider2D>();
        // disable navmesh and collider (no collider for now...)
        Destroy(agent.GetComponent <Collider>());
        Destroy(agent.GetComponent <MeshRenderer>());

        // pass serialized values once
        agent.speed                 = _speed;
        agent.angularSpeed          = _angularSpeed;
        agent.acceleration          = _acceleration;
        agent.stoppingDistance      = _stoppingDistance;
        agent.autoBraking           = _autoBraking;
        agent.radius                = _radius;
        agent.obstacleAvoidanceType = _quality;
        agent.avoidancePriority     = _priority;
        agent.autoRepath            = _autoRepath;
    }
    // monobehaviour ///////////////////////////////////////////////////////////
    void Awake()
    {
        // create projection
        var go = GameObject.CreatePrimitive(PrimitiveType.Cylinder);

        go.name = "NAVIGATION2D_OBSTACLE";
        // project object to 3D (at y=0.5 so feet are at y=0 on navmesh)
        go.transform.position = NavMeshUtils2D.ProjectObjectTo3D(transform.position);
        go.transform.rotation = Quaternion.Euler(NavMeshUtils2D.RotationTo3D(transform.eulerAngles));
        obstacle = go.AddComponent <NavMeshObstacle>();

        // disable mesh and collider (no collider for now)
        Destroy(obstacle.GetComponent <Collider>());
        Destroy(obstacle.GetComponent <MeshRenderer>());
    }
    void Update()
    {
        // copy properties to projection all the time
        // (in case they are modified after creating it)
        obstacle.carving = carve;
        // project object to 3D (at y=0.5 so feet are at y=0 on navmesh)
        obstacle.center = NavMeshUtils2D.ProjectObjectTo3D(center);
        obstacle.size   = new Vector3(size.x, 1, size.y);

        // scale and rotate to match scaled/rotated sprites center properly
        obstacle.transform.localScale = new Vector3(transform.localScale.x, 1, transform.localScale.y);
        obstacle.transform.rotation   = Quaternion.Euler(NavMeshUtils2D.RotationTo3D(transform.eulerAngles));

        // project object to 3D (at y=0.5 so feet are at y=0 on navmesh)
        obstacle.transform.position = NavMeshUtils2D.ProjectObjectTo3D(transform.position);
    }
Beispiel #4
0
    void Update()
    {
        // copy position: transform in Update, rigidbody in FixedUpdate
        if (rigidbody2D == null || rigidbody2D.isKinematic)
        {
            transform.position = NavMeshUtils2D.ProjectTo2D(agent.transform.position);
        }

        // stuck detection
        if (IsStuck())
        {
            // stop agent movement, reset it to current position
            agent.ResetPath();
            // project object to 3D (at y=0.5 so feet are at y=0 on navmesh)
            agent.transform.position = NavMeshUtils2D.ProjectObjectTo3D(transform.position);
            Debug.Log("stopped agent because of collision in 2D plane");
        }
    }