Pathfinder on Air ?

My script gives the ability to an enemy to fly and follow me, but can’t find any other ways if he blocked. I need to make a pathfinder without using “Nav Mesh Agent”.

using UnityEngine;
using System.Collections;

public class fly : MonoBehaviour {
   
    public float moveForce = 1.0F;
    public float rotateTorque = 1.0F;
    public float hoverHeight = 4.0F;
    public float hoverForce = 5.0F;
    public float hoverDamp = 0.5F;
    public float hoverError;

    public Transform target; // player
    public Transform myTransform;  // enemy

    void Start() {
        rb = GetComponent<Rigidbody>();
        rb.drag = 0.5F;
        rb.angularDrag = 0.5F;
    }

    void Update() {

        transform.LookAt(target);
            transform.Translate(Vector3.forward*5*Time.deltaTime);
    }

    void FixedUpdate() {
        rb.AddForce(Input.GetAxis("Vertical") * moveForce * transform.forward);
        rb.AddTorque(Input.GetAxis("Horizontal") * rotateTorque * Vector3.up);
        RaycastHit hit;
        Ray downRay = new Ray(transform.position, -Vector3.up);
        if (Physics.Raycast(downRay, out hit)) {
            float hoverError = hoverHeight - hit.distance;
            if (hoverError > 0) {
                float upwardSpeed = rb.velocity.y;
                float lift = hoverError * hoverForce - upwardSpeed * hoverDamp;
                rb.AddForce(lift * Vector3.up);
            }
        }
    }
}

The A* algorithm seems like a good fit, you could chunk up your world into a 3d grid, i bet a bunch of A* packages already support this, worth a search.

I personally wouldnt discount the navmeshagent, you could make an agent that acts as a guide for your flying enemy not attached to it but instead a seperate object that navigates across the world and shows where the enemy must fly. Using jump links to cross over obstacles such as cliffs.

3 Likes

I want use my own script.I used before A*, I find it difficult. The second point is really good. Yes, i can make it with and hidden object.
But still, Why there is no Nav Mesh Agent work on air ?

Mainly because you can’t make a mesh that fills a volume of empty space in a sensible way. Nav meshes describe traversable surfaces.

Do you need pathfinding for this? Do you truly have scenarios where an agent in the air is “blocked” from his destination? It sounds like just an obstacle avoidance steering behavior would do most of the work.

Yes, please. example “Ace compact” those aircraft games.

That doesn’t answer the question though. Why do you need pathfinding instead of just having agents steer towards their target and avoid obstacles.

This is the first time I read about “agents steer”. Can you give me a tutorial ?

Is your air 2D? If so just put in an invisible mesh and throw a navmesh on it.

If your air is 3D, you’ll need to roll a custom solution. Unity doesn’t natively support 3D pathfinding.

Thank you KelsoMRK

Yes, 3D. I don’t know why till now why Unity not supporting ?

Why? Because there really isn’t much demand for it.

As @KelsoMRK pointed out, many 3D games are mostly open space. So there is no need for pathfinding of any sort, you can just go in a straight line with basic obstacle avoidance.

Many ‘3D’ systems in games are just multilayered 2D systems, meaning 2D path finding works.

Its rare to find a true “The enemies gate is down” 3D game.

Thank you for all information.