I utilized the build script that I wrote for the last project and created a series of grid spaces out of blocks. I then altered the build script to fit a 2D environment rather than a 3D by adjusting the y-value offset of the normal collider to fit the z-value instead, this allows the blocks to be built on-top of the 2D plane. Because I built the grid out of blocks, the new system for character placement snaps directly to the grid without any overlapping worries. The next issue to address was the fact that the blocks placed were not going to be blocks, but rather characters and so they could not have other objects placed on-top of them. To combat this I created a new Tag for placed objects, and made sure that the script was updated to include them in the account of "block objetcs" that the script interacts with, but only allowing the script to delete these blocks.
Next, since there is no camera movement I ran into a new issue. The raycast to decide the placement location was coming from Camera.main and going to the exact center of the screen (Screen.width /2, Screen.height /2); I needed to change it to be from the center of the camera to the mouse's pointer location (this way it will be easy to correlate the mouse location for touch location on mobile devices). The string for the target location was changed from the location of half screen size to the location of Input.mousePosition. This creates a new Vector location representative of the distance between the center of the main camera and the current location of the mouse pointer.
To effectively create the illusion of grid-placement, even though the vector itself never follows exact guidelines, I created an object at the end of the raycast that snaps grid space that the mouse is hovering over and does not change until the mouse itself breaks the threshold of the grid space. At which time it simply moves to be centered on the next grid space; this object's vector location is also the location that the object is placed when the button is clicked. Because of this the mouse's pointer is only used to decide what grid space is below the current position and then factors where to place objects based on that locations center; as found by the faux objects current snap position.
Once the interface was working properly I added a resource folder and created a new object to be placed on grid-location for testing. This is easily called by Resources.Load, and then specifying the name of the object I wish to place. This will later be updated to correlate to the currently selected character using a simple array system. I then used the transform position of the faux object to tell the object where to be placed on the grid. Because the object placed is defined as being a block, but not a grid space, it can still be removed from the grid but not overlapped.
So now you can move the mouse cursor around and place objects from the resources folder on the grid space, and remove them if needed. Creating the character objects will be the next process for the character placement script, and then adding them to the array of placeable objects, and updating the GUI to allow them to be accessed by button selection. First though, I am going to take a look at the old Javascript AI system I wrote from a tutorial for my first project Extinction Plan and I am going to rewrite it in C#. This script will be used to allow the players characters to perform an attack rotation, as well as locate the enemy and rotate to face them. Because I am dealing with grid-movement, I am probably going to limit the ability for the script to move the character to rotation only, and continue working on the build script to allow the player to use it to move characters to new grid spaces as they see fit. The scripts movement will still be updated to be used on the enemy AI though, so the changes will still be made.
No comments:
Post a Comment