- Home /
Best way to handle high number of usable/pickable objects?
Hey,
I'm trying to learn how the optimal game logic should work and what are the best ways to create game mechanics in terms of performance.
I want to have a lot of pick-able objects, usable objects, containers, and "usable" characters.
Character should be able to "activate" or use them only if he is near them.
What is the best way to achieve this without making it too heavy for performance?
The simplest way that comes to my mind is to calculate distance between the character and every object every frame/time but this will be probably not optimal when there will be few hundreds of them in one scene.
Do you have any ideas how this should be build? What about the performance? How other games handle this?
Do you want usable items to be picked automatically or only upon user's input?
I want player to be able to use/pick/activate an item only when the character is in front of it and the item is near the center of a screen and the distance is less than about 2 meters.
Answer by HarshadK · Jun 11, 2014 at 11:13 AM
For:
Character should be able to "activate" or use them only if he is near them.
use trigger collider to these game objects and when the character collides with trigger perform the action that you want character to perform like picking it up or whatever.
For informaiton about triggers to start with:
2) Collider.OnTriggerEnter(Collider)
Also since we are at high number of objects one more thing to know is that don't just instantiate and destroy game objects rather use pooling for these game objects for efficient performance.
Thanks.
I was thinking about using colliders and triggers too.
The thing is I need the character to be able to activate an object only if the object is on the center of the screen and when the distance is less than about 2 meters.. What will be a better option to achieve this. By using a additional collider in front of the camera? Or maybe with a raycast from center of the screen ins$$anonymous$$d of a collider?
Distance between which two objects? Player and pickup object or camera and pickup object?
@Harshad$$anonymous$$ First Person Camera so generally same distance from player's "head" and the main camera.
I'm thinking about raycast because it always points into one "pixel" (afaik), so when there are two or more different objects near each other, the first hit is stored and it's clear which of them (objects) is in the center of the screen and which of them is hit first.
With colliders and triggers it is possible that two different objects will collide with the "character collider" that is placed for example in the front of the character. Anyway I know that I can send an event on the collider enter so just the first object that collides will be stored.
In short words. Raycast always hits only one object and it's clear which one. Collider can collide with multuiple objects at once beacause it's a three-dimensional thing in the space.
Am I thinking right?
Which option is better in your opinion? Or maybe there is some other, better approach?
In this case a raycast would be better since you only need to do it once when the player enters the trigger of pickup object. If you use collider then it will generate unnecessary OnTriggerEnter calls. Also because this is an FPS you can raycast from the position of camera since position of camera will be the center of screen and also you don't have to perform any calculations to get the center of the screen itself.
@Harshad$$anonymous$$
Thanks for your help! I will try with a raycast.