#Targeting system 3D

1 messages · Page 1 of 1 (latest)

wanton widget
#

Hey everyone.

I'm working on my first game and I'm kind of aiming for the moon. I know it's not smart but let's just say that right now I'm in the trying to figure out how to make each game mechanic or system from scratch. Learning how to code, learning how to create blender objects and learning everything along the way.

With the tools online I've been able to do a lot but I'm stumbling on a targetting system that works. I've added a screenshot of the game + some of the background information for you to check out. As well as the code I'm going to talk about.

I've added the script Targeting.gd script to an Area3D note that has a cylinder shape3D collision shape as a child parent. Every frame I calculate which overlapping bodies on collision layer 2 are within the Area3D and then after clicking "target" key input (right mouse button) I print out some of the stuff I thought I could use to make the targeting system work.

What I want it to do is calculate which object on screen is the closest to the middle (think Zelda or RDR2 style) that forces the camera to pan over to it, once close enough gives an option to talk or to do things like break a vase or whatever else I can think off.

I got to calculating the position of the objects in that Area3D but then I learned that the vector3 information is useless once trying to calculate how it looks on screen. I got to calculating the center point of the camera view which I think I managed.

When now trying to use that same strategy to print out the vector2 position of objects inside the array "detection" in a viewport I get stuck on how to do it. First of all, with everything I do I totally forget to calculate whether an object is even on screen or not. Then, I get loads of issues with vector3 positions not being suited for a camera3.unproject_position() function... I'm stuck.

So yeah if anyone has a tutorial or clue how to get it to work let me know! 😆😆

west plover
#

using screen projection to get a target is very inefficient (lots of slow maths involved), so the fast/easy way to do this kind of system is usually:

  1. store a list of all targetable somewhere in a manager (ideally all targetables register themselves when they can be targeted & remove themselves when they can't be)
  2. when you want to get a target, for each targetable in the list get the angle between the camera direction and (targetable.position - camera.position) & filter if too far (if outside of your targeting range, no need to check)

targetable with lowest angle win (most of the time the algorithm is a bit more complex as a close object will 'weight' more - but this will be a good start)

wanton widget
# west plover using screen projection to get a target is very inefficient (lots of slow maths ...

I see... Somewhere I read about managing like a group like that but I decided to go this route. Seems I was wrong unfortunately. No worries, will definitely try what you are saying.

I have a few more questions as a follow up:

  1. What should I use to store/manage all the targetables?
  2. How would the code on the second point to look like? Would it be in the Character script?

var camera = get_camera_3d()

if action_is_pressed("target"):
for i in targetable:
set target_angle = i.position - camera.position

And then how would you filter or weigh out all the options?

if target_angle >= 60 #I'm guessing it uses radius?
i.angle = null

Could this help/work?