Search Unity

Help with gid snap in game?

Discussion in 'Scripting' started by Flufferbat, Sep 10, 2019.

  1. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    Hi,

    I've for the last like 4 days been trying to figure out how to make a game like this:


    And I can't seem to find what I need on the asset store and think I might have to write my own code, I'm not sure what can help me, I started to think this was what I needed but now I'm not sure.. https://assetstore.unity.com/packages/tools/modeling/runtime-transform-handles-65363

    ok so anyway to the point..

    what I want is a script i can drop object etc into to change the values so something like

    [SerializeField]
    public/private Item_to_spawn; (on the snapped grid)

    [SerializeField]
    public/private Item_Ghost_green; (can build)

    [SerializeField]
    public/private Item_Ghost_red; (cant build)

    [SerializeField]
    public/private Restricted_Items; (items the object cant be built near) list?

    [SerializeField]
    public/private Floor; (to get the raycast)

    [SerializeField]
    public/private grid_type; (iso/rect etc)

    "SerializeFields"]
    Controls; (rotate,place&delete,menu)

    I'm new to this so I have no Idea how to actually get that ^^ to work.. I start writing that and then just don't know how to reference each thing and if i just do what visual studio says well.. that doesn't do anything lol..

    Like how many scripts do i need? what parts do I need to write them for?

    I need the Item to spawn stuff activate from GUI buttons/images & I want to not be-able to build on-top of some objects.. Also need a pipe & conveyor system and some way of making recipes trigger when the items go inside machines etc.. I would really like to get this into like as few scripts as possible etc..

    Sorry I know raycasting etc is a "hottopic" I did search for a while couldn't find anything like my question..
     
  2. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor.UIElements;
    4. using UnityEngine;
    5.  
    6. public class Gui_Slot_Handler : MonoBehaviour
    7.  
    8.     public GameObject ItemIcon;
    9.     public GameObject ItemToPlace;
    10.     public GameObject ItemGhostYes;
    11.     public GameObject ItemGhostNo;
    12.     public FloatField RestrictedZoneItems;
    13.  
    14.          //public controls for Rotate & delete functions too??
    15.         //private OnmouseDown&OnMouseUp..
    16.            
    17. // Update is called once per frame
    18. void Update()
    19.     {
    20.  
    21.     private ItemIcon =
    22.  
    23.     private ItemToPlace = (can I put the mouse/raycast here? So when I click the icon it activates it)
    24.  
    25.     private ItemGhostYes = (can raycast work for this too in this file?)
    26.  
    27.     private ItemGhostNo = (can raycast work for this too in this file?)
    28.  
    29.     private RestrictedZoneItems = (can raycast work for this too in this file?)
    30.  
    31.     }
    32. }
    33.  
    As you can see I'm really tired and don't know what I'm doing? lol I need to send all this
    { public GameObject ItemToPlace;
    public GameObject ItemGhostYes;
    public GameObject ItemGhostNo;
    public FloatField RestrictedZoneItems;}
    info to the mouse clicks script or incorporate it into this?
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,400
    I would say, you are picking too big task, for your current knowledge.
    What you want is certainly possible. But you need dissect whole concept into smallest possible bits.
    And work from there.
    Make each bit as separate prototype.
    - Mouse detection
    - Snapping to grid
    - Animations
    - Grid cells interactions
    - And many more ...

    While there are many ways of doing things, I would look into concept of storing grid in 1D array.
     
    Lethn likes this.
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    306
    To add to what Antypodish said, the general idea is called "Divide and Conquer". When you have a big problem that's hard to solve, then divide it into smaller ones and do that as long until you can conquer, or solve, each of these small problems. The sum of all small solutions is a solution to your big problem. Antypodish already gave some examples for sub-problems involved in solving your problem, however i'd suggest just starting with the smallest thing you can imagine for your game and improving from there. Planning out the whole project can be confusing for a beginner. Just pick something that's involved in your project, which you feel comfortable with, and work your way towards the goal from there.

    For example, start by adding a plane to your scene and, on left click, place some object on the plane. Then make the object snap to a "grid", by simply converting the float position to ints before placing it. Afterwards maybe change the object you place with middle mouse scrolling (sphere to square and back). Then add a visual (greenish) representation so you actually see which object you are going to place. Next actually save the placed objects in a 2-dimensional array by using the position (int, int) you placed it on, as index for insertion. Now, when you insert an item, you have the ability to check the indices around the desired location for objects it may not be placed next to. And so on, and so on.

    As you may have noticed, we are approaching something similar to the game you are looking for, but each step (i hope) is small, understandable and can be solved sequentially. If you have problems with any of these steps, then now you at least have a better understanding to google for, since it's small problems tons of people had to solve before you. Now, i already gave you some example steps you could follow, but afterwards you will have to think about the next steps for yourself :)
     
    Antypodish likes this.
  5. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    Yea I have to say not knowing what to search for is hampering my efforts.. I do understand the small steps thing, I'ts something that just doesn't seem like it would be hard.. I have managed to get a raycast a couple of days ago but yea wasn't locked to the grid.. I did find this (
    ) too its very close to what i want.. but I need the build-able's inside another gameobject like with this one:
    so i can offset it.. that first one for some reason when I do that it doesn't keep the offset.. I also wanted to avoid using blocks/tiles/planes as the floor, but I guess its just the best way isn't it.. That would have to be the thing I want the most the rest is like you say allot easier.. It's funny I thought I would get it almost instantly or it would just be something you could choose lol.. how wrong was I!
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    306
    First and foremost, it's really hard to follow your line of thought with all these ..'s in there. Also, when posting 2 videos and a line of text between, maybe leave a bit of space. Took me 3 reads until i even spotted that there was text between the videos haha.

    Other than that, i'm not entire sure i understand what your problems are, but i'll try to help with what i understood.

    When instantiating a gameobject via script, you can set its parent in the hierarchy as follows:
    Code (CSharp):
    1. newObj.transform.parent = parentObject.transform;
    You can then move everything by moving the parentObject. If i understood you correctly, that's something you want. But again, all these things are implementation details that can easily be added later on.

    What else would you use as floor? Keep in mind, we are talking about the early phases of development here, but generally speaking you could replace the plane with any other object you want. It's just that with the kind of game you are trying to create, you need some flat ground anyways, dont you?
     
  7. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    169
    What do you want to offset?

    Also, you need (not really NEED, but it's the way to go) to have a flat floor for this type of grid snapping. A plane is the simplest and best way to have a flat floor. What were you thinking as far as a floor goes?
     
  8. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    Well I'm not really surprised you find me hard to understand and that's the what 3rd post I've done not like I realised my links were going to do that lol.. And what I mean is I don't want 1000 tiles on my map before I even start putting in game objects down I figured that would actually be the wrong thing to do.. I mean I didn't really expect anything else from this forum basically just confirming it..
     
  9. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    306
    Nobody ever said you should put down 1000 tiles. In fact, the only person who mentioned the word tiles is you^^
    Putting down one plane is just simply the easiest way to simulate a "ground". And to place objects on a ground, you need a ground first. Are we maybe talking past each other? Wasnt that the whole idea? Or are you going for something more like the 3d block building in the second video?

    Also, i did not want to sound offensive or anything when i mentioned that the text was hard to spot. It was simply something that caused problems for me, and probably others, so i wanted to mention it so you can do better next time. There are way worse first-posts. You already managed to use code tags, which most first posters dont for some reason, so yeah. Did not mean any harm by mentioning that.
     
    EdGunther likes this.
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,400
    OP simply due lack of experience, has limited programming and technical vocabulary. Nothing wrong. All can be learned in time. Language may be another thing.

    But OP wants to make building and quite technical type of game. Is like factorio in 3D, or Minecraft with mods. There are plenty other examples.

    Problem with OP approach is, it takes again too big chunk that can bite. I would drop completely 3D for now, and focus on 2D tile based game. Even relative simple game play, when something can move across tiles. This approach will allow understand, how tiles can be correlated between each other. Including neighbors.

    In fact, making Tetris or snake like game, would be good starting point. It involves basics aspects of tile based games.

    Then could possibly look into early Simcity type games.
    Keep it really casual and small. Like 10x10 at most, to start of.
    All in 2D.

    Being able scale later and converting to 3D tile system, will be much easier.

    Minecraft techniques can come to help.

    However, there is too many topics involved in such game making, for such vauge topic description. Specially for beginner.

    Yet seems OP is looking right direction, for tile making.
    Anything with 'tile'/'grid' based keywords could do. But there will be lot of research and testing involved.

    One thing need to be learn along the line, how to manage renders objects, avoiding instantiating what not necessary.
    Arrays and list can help here alot. Then GameObjects pooling. That just small drip in the ocean of challenges.

    And again, keeping small, allows to avoid some of these challenges. At least for now.

    Good luck :)
     
  11. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    Right well that is something I wanted to know, but when you said place a plain I automatically thought you then meant to keep placing more because like the next guy says it's not an easy thing to search for and yea you clearly know why.. And it's also the reason they won't put a snap to grid option in.. Because theirs countless ways of doing it and like I feel like you guys totally ignored my first line.. "I've been trying to figure this out fot 4 days. Anyway.. Why do ppl use heaps of cubes or whatever when you can just use a plane? Also I have obviously downloaded a few examples.. I actually found an asset that almost what I said originally except its really old and it's been abandoned so I'm not going to buy it and also don't really think it's something worth paying for.. At least now I know I can just use a plane my only real question about that is how do a make it still snap to a grid with just one plane? And one thing I watched the guy was like I hate using planes coz it's made of heaps of triangles.. Oh wait that's why it works is it?
     
  12. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    Not really sure how I want the floor to look was just going to use those level prototype materials you can get on the asset store. .Although the wood floors pack I found last night seems pretty nice would look silly on a grid pattern lol but good for a plane probably. Oh and um if I wanted to do this in 2d I have rpg maker mv & the build plugin. I could easily do that their but I don't want to and that's Java and also pretty much just choose how you want stuff to behave..
     
  13. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,400
    The snap is really the easy part. Is simply rounding the position. Lets say you have object at position x=3.2f you round it, and you got object at position 3. If x=3.7f it will snap to 4. It is juz basics math. You may want add offset if relevant. I am not going to 3d topic, as you probably will need 'normals', which is higher level math. But also Unity provides relevant functions.

    Btw. Because RPG maker was in 2D as you mentioned, doesn't mean it explain the technical concept of tailing. It would be stil recommended route to go 2D. Or you will soon hit range of overwhelming tasks, risking hitting wall and eventually giving up. It is very common, due to lack sense of progression.

    You see, you already stuck few days on one small feature. And such challenges will be showing more, while tutorials will become less adequate as you will progress.

    Do small prototypes. Test own capabilities, and try do some simple 2d tiling mechanics, with neighbors detection. It will be really good learning curve for you. Then expand.
     
    Yoreki likes this.
  14. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    59
    Yea as others have said, grid snapping is easy. I'm making a procedural generated platformer like terraria, and to place blocks i take the mouse position, round it to an Int and that gets you the position you want to place the block. You can then use this position to check if other objects are already there, if its too close etc. Just make sure the center of each grid cell is a rounded digit, so (1,1), (1,2) etc are grid coordinates. To ensure this make sure you round the position to ints. I use Mathf.round()
     
    Antypodish likes this.
  15. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    306
    Dont wanna be that guy, but it's not like nobody explained the whole grid-snapping part before.
    When you complained about us not properly reading your first post, i found that a bit frustrating to read to be honest.. :/

    But yeah "grid snapping" is effectively just converting the coordinates to ints in a way that fits for your game. Technically this conversion can be done in any way you like, but for most applications normal rounding should feel the best.

    As we tried to explain to you, you really need to start small. As small as you can imagine. And then always figure out how one atomic feature can be added to your game at a time. It's also not a bad idea to not directly work on your game, but instead make prototypes for some features instead. After you've done it once, you can basically repeat it in very little time. It's the "figuring out how" part that takes time. I'll mention again that i already posted some more or less atomic steps to start this project in a previous post. If you have troubles with any one of these steps, you should be able to find a tutorial on each of them, since i already selected them in a way that makes googling for them rather easy, imho.

    If that's not enough, or not a good way for you to start approaching the task, then i'd suggest the same others have said multiple times now: look for some tutorial that's about a grid-like game or just grids in general, follow it (by actually typing, not copying, code) and try to understand what you are doing, and why it works. Afterwards you should be able to apply the learned knowledge to your own project. Rinse and repeat for each new problem. If you encounter any other problems you cant solve yourself, then we are still here to help you tho.
     
  16. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu(fileName = "Machine", menuName = "Machine")]
    6. public class Create_Machine : ScriptableObject {
    7.  
    8.     public Sprite Icon;
    9.     public GameObject Boundry;
    10.     public string MachineName;
    11.     public string MachineType;
    12.     public string MachineTask;
    13.     public GameObject PropToUse;
    14.     public int CostToBuild;
    15.     public int RepairCost;
    16.     public int RefundAmount;
    17.     public int IsItemLocked;
    18.     public Material BuildGhost;
    19.     public Material RestrictedGhost;
    20.     public int RotationAmount;
    21.     public LayerMask Layer;
    22.     public int Power;
    23.     public int PowerUse;
    24.     public int Mana;
    25.     public int ManaUse;
    26.     public int DarkEnergy;
    27.     public int DarkEnergyUse;
    28.     public int Liquid;
    29.     public string LiquidName;
    30.     public string LiquidType;
    31.     public int LiquidUse;
    32.     public int MutatorStorage;
    33.     public string MutatorName;
    34.     public string MutatorType;
    35.     public int MutatorUse;
    36.     public int Health;
    37.     public int DOT;
    38.     public GameObject[] ItemsToMake;
    39.     public GameObject[] ItemsToConsume;
    40.     public ScriptableObject[] Recipies;
    41.     public Color Highlight;
    42. }
    43.  
    ok well Im not going to even bother reading stuff when it starts with crap like that..
    anyway looks like I figured this much out at-least.. and yea like i said right at the start of my post.. I looked for an example like what i wanted and couldn't really find it but I mean after 5 days of looking and some of what u guys said I am understanding the whole raycast to grid thing.. and really yes all I was asking was how to do the grid if i should use planes or not etc and how to do this ^^^ which i know now how too.. and i think thats a pretty good machine maker that.. that's actually usable btw.. thing is now I need to figure out how to call back to all that but thats what youtube is for huh.. like the thing is you guys are just being rediculous and rather than focus on the fact I'm a noob you shoulda just answered the question.. and if you didn't understand it why answer in the first place, this isn't a conversation about my mental capacity or life situation etc.. so how bout sticking to the subject and avoid getting done for flaming..

    So is any of that wrong for Eg.. the int's strings and object types etc? should I use anything differently from what I have?
     
    Last edited: Sep 11, 2019
  17. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    306
    Let me get this straight. Your title is literally the only thing that specifically just asks for a solution to grid snapping. In your post you talked about how you wanted to create a game similar to the video posted, but - and this is a quote - "are very new to it, have no idea where to start, write some variable declarations and already dont know how to reference them".

    So we then rightfully arrived at the conclusion that you are new and need detailed help. So we explained to you how to break down the problem, but how a project like this may be a bit too much for a total beginner, and that you should start with small prototypes and whatnot. Correct me if i'm wrong, but from my perspective we gave very reasonable advices, that were reasonably easy to understand from a beginners point of view.

    You then state your concern that you feel like we ignored your first line. Fair enough. Your first statement tells us you've been at it for a couple days. So what? I mean, this doesnt really give us a lot of information.. how would not ignoring it look like? I then mentioned that it's a bit frustrating to be accused of ignoring what you wrote, as a reply to you asking again how grid snapping is done, after it was answered like 10 posts ago. But hey, it's fine as everybody can miss something in all this text. So i went into more detail for how grid snapping works, and how you can try to approach the problem you are facing.
    But i doubt you read it, since - quote - you didnt bother reading something starting with "such crap".

    And now we are here. Where your newest post includes, but is not limited to:
    Telling us we are ridiculous, calling us flamers, claiming we did not focus on answering your question, implying we should not answer at all if we dont understand what's being talked about, and so on.

    Mind your manners man, that's not very nice. We are here to help you, not to get accused by you. You came here with more than one question. We answered all of them with appropriate advice aimed for a beginner. If you believe everyone here misinterpreted your question, or did not focus on what's important, then maybe think about why that may be. It's not always others who are at fault. If you just want to talk about one topic, then dont bring up the others and complain when people actually start offering advice on more than one topic.
     
  18. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    Ok well I read it now.. What is it that you think I should search for 3d?

    Anyway I kept going but ran into a couple of problems one I changed a bunch of those GameObjects to ScriptableObjects and then realised you can't drop them onto emptygameobjects then though ok well I'll just try and do the instantiate to mouse thing without placing it so I could put that on a button and made one of those create ScriptableObjects things CreateToMouseEvent and yea started by just adding the camera.. Then went back checked on yt to try and find something and what I looked at had a custom class in it which rekt me tbh lol.. Coz dude musta had it in a different file and I didn't notice lol. anyway I then realised while typing out my version of what I wanted.. That he had used that custom class as a list[] and somehow it was a GameObject.. Anyway I then realised that I now somehow have to get the GameObject prefabs and materials from inside the scriptable object to send to the mouse to display the green & red build versions and build it obvious.. And yea I have watched a few videos on rounding Down now it's just hard to take it in I think I just need keep watching vids on it.. I did find on interesting asset that's fairly new that is for making tilemaps.. I know I said I didn't want to use them but I still need to round if I do huh.. Anyway the asset seems to do all the math for you as in sends the info to the mouse I think? I'll have to watch the video again.. Not 100% what I want but it might be useful I will link it, also let's you paint your tilemap layout and tweak all the tiles position and lock tiles together while doing so etc.. I think this guy who made it is onto something for sure help ppl like me.. Anyway I was thinking if I get this basic system working I might release it before I start actually making the game. .
     
  19. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    I was setting the scene lol.. Chill man
     
  20. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    I don't see how asking ppl to stop talking to me like dirt is #@&# taking? Lol
     
  21. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23

    This is my first project in rpg maker..
    I actually had to write a few bits for this one.. If you ask me I have a cool concept I changed the tech points into chaos and changed the math.. Most of the animation is mine except for the actual player and bird well I added a new animation to the player model for casting coz it was set up as an malle enemy not player..
     
    Last edited: Sep 11, 2019
  22. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    something else I do is use vdj and well to get your controllers working you need to do code.. You can also code milkdrop files like this.. Which is another code again..
     
  23. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    I started learning HTML in highschool back in 1997..

    I also use other code of many types https://steamcommunity.com/profiles/76561198099710707/myworkshopfiles/

    Here is my workshop.. I've only recently started using it, I used to mod all sorts of other games Inc oblivion & a fake server version of need for speed world.. I even used to make bots for diablo2.. I've also actually made my own vst before.. And I mean I actually have done what was said and wrote that file and 3 others just to get some ideas down no I don't know how to reference them and use the info properly... YET!
     
  24. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    306
    I dont know how you got this idea, but we are not trying to talk to you like dirt. As far as i've seen, you've always got nice replies from the people of this forum. Please try to refrain from double (or hexa) posting tho. It makes it really hard to follow the thread, and you just posted 6 timed in a row over a span of 15 or so minutes. You can always use the edit function if you need to add something to your posts.

    Now, this may be on me, but i believe i got lost a bit as for what you need or dont need help with anymore.
    Do you still need a working example for basic grid snapping? Or is that off the table and you are mainly concerned with changing the model / material based on the state (preview vs placed)?
     
  25. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    All good man, I will figure out the references myself that's not really hard like it will be only one line I need each time, but yea the snap grid with rotatable objects referencing that file for the rotation amount..

    My next step obviously need to be just getting the object (and change the material) to the mouse via a on click script on a button be that an object button using an asset which I have gui button.. Which for some reason don't seem to be highlighting/selectable pretty sure it's coz I haven't done my layers properly and I dunno why it looks different to me that section in 2019 compared to 2018.. Any bugs I need to know about?

    Oh and I have actually managed once while trying to snap getting an object to follow the mouse that's pretty easy that bit.. But yea it all depends whether or not the video I'm watching is from a button or by pressing a key or clicking on the item then making a Clone.. I'm just going to sleep so yea..
     
    Last edited: Sep 11, 2019
  26. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,400
    I see OP has moved from grid design, to all over the place, to be honest.

    I suggest either stick to original post, as requested, or changing the title to something more adequate.

    But in fact, I would just leave this topic, since my best assumption is, OP got what wants. Either via vids, or assets.

    Then if question is regarding different aspect than grid, is healthier for a thread, to find, or if not there, then create new thread, which discusses specific problem.

    That will help also future readers, to follow what is going on.

    Alternatively, OP can request, or create thread in WIP forum section, where is more suitable on discussing project's progress and workflow.
     
  27. Flufferbat

    Flufferbat

    Joined:
    Apr 11, 2019
    Posts:
    23
    Blah blah blah dehibidy blah..
    Kinda funny considering I actually haven't said really anything different from my original post ay. Maybe you should read it again.. Nothing in my plan or request has changed except for the use of scriptable objects..
    Which I only just said lol..
    Oh hey would you look at that an ignore function hope it actually works..
     
    Last edited: Sep 11, 2019
  28. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,400
    No I wont, I have seen original post before editing. Relevant action has been taken, because such behavior is not acceptable on this forum.

    All best in dev journey.