Trying to write generic code for a board game - c#

I'm actually working on a board game using unity. It's a board game where players have to connect two opposites borders by claiming empty hexagonal cases. Borders are assigned to players at the beginning.
I chose to represent my empty cases with gems, white when unclaimed, and red or blue for players. So I made a single prefab for all gems.
To check connections between gems themselves and with borders, I assigned to them slightly bigger box colliders so they are colliding (trigger) with other objects, so I can detect if a claimed gem is connected (directly or not) to a border. I wrote this code generically as gems are one single prefab, naively thinking that every entity would run its own version of the script.. But that's not the case.
So I'm looking for an alternative way to detect connections with borders, but still generically, as the size of the board is variable.
Thanks in advance !

Your problem is closely related on how you break your game in game objects and components on Unity3D.
First of all, use scripts to define components. For example your gems could have a script that contains data related to its state (your white, red and blue gems meaning empty, playerA and playerB).
You will probably need a game object (normally called a GameManager) to handle all behavior related to game rules like which player is playing, if a player can pick a specific gem and if a player won the game. Depending on the game complexity is a good idea to also have a game object with a component (script) to keep your board state, instead of simply getting it from the gems objects Transform component.
After you have structured your game you do not need colliders to detect connections. Every time you have to check for connections, just iterate over your gems or use your board state object.
Edit:
References to help you develop your game:
How do I represent a hextile/hex grid in memory?
Creating a holder for game level for simple board tile based game.
creating 2d table\chess board\2d array

Related

Object too fast for Collision in Unity

I'm currently making a simulation where disks are placed in a conveyor belt and sorted by color. When the sensor detects a white disk, a rectangular object 'pushes' out of the conveyor belt into a box, and it needs to move quickly. However, whenever we set the speed to a high number it just goes through the disks, without pushing them. I have already tried making the pushing object's collision detection 'Continuous dynamic' (RigidBody) and the disks' collision detection 'Continuous' (like in this video: https://www.youtube.com/watch?v=XvrFQJ3n8Mo). I have attached an image of how the mentioned part of the robot looks and the settings for both the disk and pusher object.
disk and pusher object settings
visualisation of robot simulation
Your problem has been solved HERE , in short it is because only convex mesh colliders can collide with each other. (Your right one is convex and the left one is not).

How to validate chess moves with a server? Winboard and Unity 2020.3

I'm looking for advice on how to implement online chess move validation using a winboard chess engine. I am creating a mobile battle chess game in Unity 2020.3. Right now I have the possible moves for each player, castle rights, check status, etc being generated by an open source C# chess engine. I chose this engine because it's written in C# and the code could be directly included in my Unity Project. I successfully wrote my own code that connects this chess engine to a 3D battle chess game.
How the game works (video preview):
A piece is selected by the player clicking it with their mouse or
tapping the screen.
The possible moves for the current board are calculated by the chess engine.
I use the possible move indexes to show possible move highlights.
When a player makes one of these possible moves, the move is applied on the chess engine.
It becomes the other player's turn.
I have some custom code to run player animations, based on the kind of move (ex. an attack animation runs when a piece is captured). I also show have screens that show when it's time for a promotion, a player is in check, or the game is over.
I'd like to somehow make the process of calculating possible moves and applying moves to be done on a server. My goal is to protect from players sending illegal moves to the board and breaking the game.
How would you recommend I approach this problem? This is the last thing I need to turn this game into an enjoyable online experience. I just lack the crucial skill of understanding how to make this game online.
On the server, you'll have a separate version of the chess game running.
A player(/client) will send a move to the server (so a unique piece ID, and the target square the player wants to move).
On the server-side, you'll need to check to see if that move is indeed possible. This could be by getting all possible moves from that piece, and checking to see if the intendedChessPosition can be found in the list of possibleChessPositions that was calculated on server-side. Only then, will you actually move the piece server-side.
All players/clients will communicate using the server as the middleman and authority.
The exact implementation could change depending on the API of the chess engine you're using, but that's the basic idea.

How to always get the AR design in front of the spatial walls Unity HoloLens

I am building an app for the HoloLens gen 1 device using Unity 2018.3.13f and MRTK V2 RC1. I got a simple AR design with 2 text objects and 1 rawimage object. After building the project and deploying it to the HoloLens the AR objects ends up behind the spatial mesh (you know all those spatial triangles), but I want all the objects to be in front of the wall.
How do I accomplish this?
The canvas is set to be on the main camera
I have the original settings for the DefaultMixedRealityconfiguraitonProfile if there is something there that needs to be changed.
This is how it looks through the hololens with the app when it does not show the mesh of the wall (sorry for the bad quality)
and this is how it looks when it falls behind the mesh
Do I need to add some mesh renderer or something on the MainCamera to make this possible?
Any help is appreciated, thanks!
I don't believe that the MRTKv2 as of 2019/5/9 has code that will auto-ensure that a specific object is positioned in between the camera and other arbitrary meshes and colliders (i.e. the spatial awareness is one such mesh, though you could imagine just having an arbitrary box or plane in the scene that would occlude that object, in which case, maybe you'd want your "in between" object to stay in between both those two types of potentially occluding things).
There used to be a script in the HTK called Tagalong.cs that would do something like this by doing raycasts from the camera to collidable object:
https://github.com/microsoft/MixedRealityToolkit-Unity/blob/htk_release/Assets/HoloToolkit/Utilities/Scripts/Tagalong.cs
This single large script I think got broken up into smaller scripts (i.e. specific behaviors in the solvers here:)
https://github.com/microsoft/MixedRealityToolkit-Unity/tree/mrtk_release/Assets/MixedRealityToolkit.SDK/Features/Utilities/Solvers
However, from what I can tell, the specific interaction of "keep things automatically between the camera and whatever collidable object" wasn't preserved. Someone else can correct me here if I'm wrong, it looks like this wasn't a behavior that got preserved in V2.
Going forward, there are a couple of possibilities:
1) Probably file an issue on Github here (https://github.com/microsoft/MixedRealityToolkit-Unity/issues) to request this feature be ported over.
2) Use the code in Tagalong.cs to add your own solver that would accomplish this (i.e. the code looks to be all there, there's just some work needed to get done to reorder it to handle what you want)
If you use a sprite renderer, set order in layer (into 0 or -1).
If you use a mesh renderer, try to deactivate dynamic occluded.
Try to change the hierarchy of the sorting layers under Edit-> Project Settings -> Sorting Layers

How to create multiplayer / shared AR game with Vuforia?

I would like to create a shared AR game on Android phones, where I would like to:
spawn a cube for each player on an ImageTarget
allow them to control the position of their cube
allow them to see the movements of all players' cubes
I'm using Vuforia as my AR library and PUN 2 as my networking library. I have no issue synchronizing the positions and rotations of all cubes. However, the cubes do not stay on the ImageTarget properly and "jump" around. On the other hand, if I place my two phones very close together and point them at the ImageTarget at roughly the same angle, the cubes do not jump as much.
This leads me to think that the 2 instances of ARCamera fail to realize that they are pointing at the same ImageTarget from 2 different angles, and instead think that the ImageTarget exists in 2 different orientations at the same time.
Is there any way for me to tell Vuforia that I'm using multiple instances of ARCamera pointing at the same ImageTarget? (Or if my hypothesis is completely wrong, how do I actually make a multiplayer AR game?)
Thanks so much in advance!
p.s. I know the Vuforia forums are a better place to ask this question but unfortunately that forum is not particularly active, so I'm trying my luck here.
I've solved the issue by going to the ARCamera gameobject, then in the Vuforia Behavior component, I changed the World Center Mode from DEVICE to FIRST_TARGET. This allows multiple instances of ARCameras to be in different positions.
More info on World Center Mode can be found here.

C# XNA Enter Houses

I'm making a role playing game in C# using XNA. I already have a map and some stuff, but that's not interesting at the moment. My question is: How can I give the player the possibility to enter houses or rooms?
To create the worlds, I've used standard int-arrays where each number represents a different type of tile. That works all fine, but the house isn't enterable but a solid textured block of something.
BTW I've used a Vector3 to determine in which world the player's currently located and which one the program must load next.
Any suggestions how I can make the houses enterable?
One easy way to make houses enterable is to create a trigger object at the door of the house.
This object can be a simple Rect along with an id.
While the player moves around your map check for a collision between the player and the trigger.
When the player enters this trigger you can change the displayed map with a new one (the interior of the house) and move the player where the door should lead.
If you're using C# to make a game, you might want to consider switching to MonoGame (which is practically XNA's successor since XNA is dead) or Unity.
As mentioned previously, your best option in XNA is to create a Rectangle which will trigger on collision and change the game world.

Categories