1
\$\begingroup\$

In a 2d top-down game, what would be the best approach to Instantiate a GameObject in front of my player as he moves around and I, for example, plant a tree.

I was thinking about creating a small trigger-collider that will always be 1 unit in front of my player, then get the Mathf.Round position of the ground-object that collider is currently overlapping, and place the tree there.

However the more I think about it the uglier that solution seems, is there some better way? Performance wise and just design wise. Maybe I could somehow get the wolrdposition of my player, his direction, and the amount of units in front I want to place the object (1), and Instantiate it there. But with this approach I cant check if I already planted a tree in the location, since I dont have a collider. i.e this approach would not know what GameObject is in front of the player.

Is my first approach okay or is there some better way?

\$\endgroup\$
1
\$\begingroup\$

From your description of a 'ground-object' I am under the impression that your game is tile-based and the tiles have information on what objects the tile may contain? If that is the case, the tile should be aware that the player is on it, or the player should be aware of the tile he is located on. Use that information and the player's facing information to get the direction you would like to check tiles in. Check the tile if it contains an object already, and if not plant the tree. If I am misunderstanding please provide a little more information on what a ground-object is and how you are storing location information.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Good point, left some stuff out there. Yes it is tiled based and yes the ground tiles do have collider-triggers and can indeed detect what object is on it. And the Player can detect what tiles he is currently on, however, with this approach the player could be standing at the intersection of 4 tiles, since the player collider is 1 unit large. how would it know which ground tile to pick? \$\endgroup\$ – Green_qaue Jun 13 '17 at 16:32
  • \$\begingroup\$ I see, so the ground is tile based; however, the movement is not. Easy enough, just create a vector based on the direction the player is facing from his center. Whatever tile the vector collides with first is the tile you check to plant the tree on. Another option is to figure out which tile the player is currently occupying the most with his collision and using that as the tile he is "bound" to for the sake of tile interactions. \$\endgroup\$ – Taitu Jun 13 '17 at 16:50
  • \$\begingroup\$ Ill play around with ur ideas tomorrow if I get time, but makes sense :) \$\endgroup\$ – Green_qaue Jun 13 '17 at 17:12
  • \$\begingroup\$ I think your comment is pretty much the same as my first method. But instead of putting a collider 1 unit in front of the player, you put it on the player and get the gameobject that is 1 unit in front of the player. \$\endgroup\$ – Green_qaue Jun 13 '17 at 20:01
  • \$\begingroup\$ It's very similar in implementation; however, it should be a little more elegant since you do not have to create extra collision objects just for this specific case. Also being able to associate the player with the tile he is standing on may prove useful for other elements you may be planning to include down the line that also interact with the tile map. What if say for instance you need to check behind the player for something? Using the collider you would have to create another collider to check for this. If you know where the player's tile is though you can just check the tile behind \$\endgroup\$ – Taitu Jun 13 '17 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.