0 votes

How can I create a selection rectangle, and select the units inside, in a 3D RTS? The following are not separate questions, but elaborations of my confusions and ideas of how such a system could work:

Is there a way that I can get all 3D objects inside of an area on the screen/viewport?

Is there a way that I can raycast from the screen into the world?

Should I create a rectangle on the viewport with Control or Node2D nodes?

I did find this question https://godotengine.org/qa/1793/rts-selecting-rectangle, but it only has answers for 2D.

in Engine by (110 points)

1 Answer

+1 vote
Best answer

Hi, as I comment in the post that you reference, you need to get a rect2 box first to make the check. Then, you has to unproject the position of your character from the camera object:

var character_pos = character.get_translation()
var unprojected_pos = cameraObj.unproject_position(character_pos)

With this and your preview rect you can check if is inside of the rectangle.

var rectSelectionBox = Rect2(bla, bla,..,..) #fill with  correct values ;)
var contains = rectSelectionBox.has_point(unprojected_pos)

Check the steps in the other post to get the rect2, that was I did and works. All this was made in Godot 2, so , I don't know if will work in Godot3.

by (50 points)
selected by
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 Frequently asked questions and 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 [email protected] with your username.