How to click on a Gridmap?
Asked Answered
C

7

0

Hey all, working on a simple click to move game, using a GridMap to do level building which is working wonders for me, but I can't seemt o figure out how to get it to work with click to move. I've followed a few tutorials, but they don't work with a GridMap. Here's what I've got so far (give me a minute to get the formatting correct, still new to the way Godot forums does their stuff)

func get_world_pos() -> void:
var mouse_pos = get_viewport().get_mouse_position()
var ray_length = 100;
var from = camera.project_ray_origin(mouse_pos)
var to = from + camera.project_ray_normal(mouse_pos) * ray_length
var space = get_world_3d().direct_space_state
var ray_query = PhysicsRayQueryParameters3D.new()
ray_query.from = from
ray_query.to = to

var result = space.intersect_ray(ray_query)
print(result.position)

navigation_agent.target_position = result.position

usually when I try to make this work and click on a GridMap object, I get this error:
"Invalid get index 'position' (on base: 'Dictionary')

Any ideas? Any tutorials on this?

Classis answered 19/10, 2023 at 14:47 Comment(0)
C
0

Ok I tried changing the code to the code from the documents:

`var space_state = get_world_3d().direct_space_state
var mousepos = get_viewport().get_mouse_position()

var origin = camera.project_ray_origin(mousepos)
var end = origin + camera.project_ray_normal(mousepos) * 5000
var query = PhysicsRayQueryParameters3D.create(origin, end)
query.collide_with_areas = true

var result = space_state.intersect_ray(query)
print(result)`

which seems to be working just fine. The only problem is that if I click on anything from my GridMap, it just returns { } but if I click on other things I get actual positions in the world. So I have no idea what I can't click on GridMaps.

I have also seen things about not being able to attack scripts to GridMap objects, is that true? If I want to add a script for something like a resource rock that a player can interact with, will that not work with GridMap? Is that true or old info?

Classis answered 19/10, 2023 at 20:6 Comment(0)
V
0

So your intesect_ray is returning {}? That means it isn't colliding with anything. Do your GridMap objects have collisions properly set up?

Villagomez answered 20/10, 2023 at 0:20 Comment(0)
C
0

Villagomez Probably not lol. I will check that and see if its the problem later. But that looks like the link that I need to get it done right! so thanks!

Classis answered 20/10, 2023 at 14:5 Comment(0)
C
0

Villagomez That seems to have worked! Well it has worked for my flat plane ground object at least. So progress!

The link you sent shows there is a way to have the collision automatically set based on the mesh, but I can't find that ability anywhere. The docs show a button, but in the editor that button doesn't appear to exist, and I don't see anywhere else where that functionality would be. Did they move it to a new spot?

Classis answered 20/10, 2023 at 14:20 Comment(0)
V
0

I'm using 4.1.2

Villagomez answered 20/10, 2023 at 18:50 Comment(0)
C
0

Villagomez I just upgraded! I think I am seeing the problem I had before, and how to properly bring in the meshes. I downloaded the tutorial from the docs and that helped too. Thanks for all the info!

Classis answered 20/10, 2023 at 19:29 Comment(0)
C
0

Villagomez
I actually found the answer to this question lol. Thanks for all the help though!
One last question, I am trying to compare the tutorials and documents and get my version working and noticing a slight discrepancy.

When I go to create a mesh library, it only exports a single room object, not all of them. I am not really sure what is causing this, but I do see that if I open my model (the blender model which has all the rooms in it) I have to open it as a child of a 3D node, but in the tutorial files all the room meshes are already in a scene as a direct child of the root node. So it looks like the system will only export things that are diret children of the root node. So how would I get all of the room meshes to be a child of the root node, and not a child of a child of the root node?

I am just not finding proper information for going from an exported model to the mesh library.

NVM, I found it! Just had to click "New Inherited Scene" and that opened it properly.

Classis answered 20/10, 2023 at 19:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.