This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Navmesh with RigidBody

MagicLordMagicLord Posts: 578Unconfirmed
edited February 2018 in 3D

Hi there,

I made a new version using RigidBody for each navmesh moving entities.

So you get some sort of characters avoidance because each rigibody will collide with others.

Download demo

https://github.com/DevMagicLord/Godot3/blob/master/navmeshWithPhysics.zip

Move player ball with with Z,S,Q,D keys
Press Space to get navmesh character move to player position

The code has been rewritten.
Some additions are possible, smooth mesh rotation towards the direction movement, deceleration.

extends RigidBody

const SPEED = 3.8;

var maxVelocity = 4.3; 

var path = [];

# Store player node to get it's position
var player;

#Navigation node able to calculate path
var navComponent ; 

#Switch navigation move 
var navMeshMove = false; 

var maxPathIndex = 0; 




func _ready():
    set_process_input(true)
    player = get_tree().get_root().get_node("level").find_node("Player");   
    navComponent = get_tree().get_root().get_node("level").find_node("NavigationLevel");
    navMeshMove = false;




func _process(delta):

    #If path has been requested must call move along calculated path function
    if(navMeshMove):
        navMeshMove(delta); 




func _input(event):

    if(Input.is_action_pressed("jump")): 

        #Request path from Navigation node
        path = navComponent.getPath(self.transform.origin,player.transform.origin); 

        #Path has been requested, move along path
        navMeshMove = true;

        maxPathIndex = path.size() - 1; 




func navMeshMove(delta): 

    var nextPos = path[maxPathIndex];   

    var dir = (nextPos - transform.origin).normalized() * SPEED;    

    if linear_velocity.length() < maxVelocity :
        apply_impulse(Vector3(0,0,0), dir);

    #global_transform.looking_at(nextPos, Vector3(0, 1, 0));

    var distanceToPoint = transform.origin.distance_to(nextPos);     

    if distanceToPoint < 3:

        maxPathIndex -= 1;      
        if maxPathIndex < 0 :
            navMeshMove = false; 

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.