0 votes

I have taken the 3D Navmesh demo (for a point and click game) and have tried adapting it to my needs. While this did work nicely with my setup (in pointing to a place on the map, clicking, and moving the node), I needed some form of collision detection so that the player node knows where not to go. I do not want to run Area checks on all the meshes for which I need detection, so I've tried to get point and click controls working with a KinematicBody.

After a suggestion from KidsCanCode in using one of his code recipes, I have cobbled together something which may work.

The node tree for my setup looks something like this:

Scene tree

The "Main" node is a Navigation node, all of the nodes under the "Terrain" node are meshes, and the "Qbit" node is a KinematicBody node. I'm trying to move "Qbit" around inside the NavigationMeshInstance. When I click to move the node, "Qbit" only moves once, and doesn't move again. I'm not entirely certain, but I think it has something to do with detecting how far the node has moved.

Here's the code I have on the "Qbit" node:

extends KinematicBody

const SPEED = 3.0
var index: int = 0
var velocity: Vector3 = Vector3()

# For pathfinding
var path
var MainPath
var beginning: Vector3
var ending: Vector3

func _ready():
    MainPath = get_node("/root/Main")

func _physics_process(delta):
    if !path:
    var target = path[index]
    var distance = translation.distance_to(target)
    if distance <= 2:
        index = wrapi( index + 1, 0, path.size() )
        target = path[index]
    velocity = (target - translation).normalized() * SPEED
    velocity = move_and_slide(velocity)

func _update_path():
    var temp_path = MainPath.get_simple_path(beginning, ending, true)
    path = Array(temp_path)

func _input(event):
    # This fires when the player clicks the left mouse button.
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
        var from = $"../Camera Base/Camera".project_ray_origin(event.position)
        var to = from + $"../Camera Base/Camera".project_ray_normal(event.position)*100
        var p = MainPath.get_closest_point_to_segment( from, to )

        beginning = MainPath.get_closest_point( self.get_translation() )
        ending = p

And just to give some perspective, here's a screenshot of the game:

Qbit in Squaresville

in Engine by (2,232 points)

It looks like the problem came from the player character's bounding box being too large. Though this has really helped my progress in implementing point and click controls, I'll still don't have exactly what I want.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.