Code Monkey home page Code Monkey logo

unity-vector3.distance-alternative's Introduction

Vector3.Distance-Alternative

Use this extension method for agents in your scene, if you have no need for the exact distance between your agent and the objective, but still need to know if it is in range.

This is not a replacement for the Distance method, but an alternative. Vector3.Distance is a great method for retrieving the exact distance between targets, but most of the time this information is not neccessarily required for the functionality of the methods it is used in.

Example: image

In this example we need this platypus to know when it is in range of the plant for it to begin eating it. The easy and common method for this would be to check the distance between these gameObjects using Vector3.Distance. This solution works, but it is very bad for performance. Vector3.Distance utilizes the square roots of the Vectors and although square roots are not as expensive to use now as they have been, we can do better.

Using this extension method in an if statement instead, we can check if is in range or has reached its destination using the integrated Unity methods of the Navigation.AI namespace. This way we are only using the information that the agent already updates to be able to function on its own, subsequently makes our Update loop much more performant.

As this is an extension method, you can use it on your NavMeshAgent reference, as seen in the Example.cs file.

unity-vector3.distance-alternative's People

Contributors

thomas-markussen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.