Detect line of sight and distance between two object in TCP Server - c#

I am developing a 3D multiplayer game. I use Unity 3D as a platform to develop my Game Client. I have to build MySQL C# Console Application which serves me as a TCP server. I am connecting my client via TCP protocol to my Server application. I have the following scenario:
I have two players inside the game both wearing guns and trying to shoot each other.
Between them, there is a wall and Player 1 receives an error when trying to shoot Player 2: "Your target is not in the line of sight." (Normally because there is a wall between them)
However, Player 2 is using "hack" and he removes the wall so he does not receive this error and kills Player 1.
My first question is:
Obviously, it's not a good idea to count only on the client to decide if there is an object between two players to fine and error. What can I do in order my Server App to be able to detect that there is an object between the player and the target? What can you suggest?
My second question is:
How can I calculate the distance between two objects which are in my Server App? Is it possible and if so how?

I imagine that in your server you can access the data of all the users in your Game World. So what you could do it, every time a user claims a hit has been done, you double-check it in the server if that is actually possible.
To do so and find out the distance, you can user Raycasting:
Vector3 direction = (...);
RaycastHit hit;
Vector3 convertDirection = transform.TransformDirection(direction);
if (Physics.Raycast(transform.position, convertDirection, out hit, Mathf.Infinity, layerMask))
{
Debug.Log(hit.distance);
}
Edit: As Rafalon suggested, not sure how cost-efficient is to do this server-side. But you can do it. After all, you are Ironman

Related

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.

Real time sync in Unity using Photon Unity Network(multiplayer)

Im working on a demo project, where I use Unity 3D and Photon Unity network(PUN) for a real time game.
The person in the top panel is an enemy unit, controlled by another player who does damage to the player on the bottom left (1/3 hp left).
However, the person on the bottom right is a healer who can heal the bottom left player (both controlled by the local player).
Here's my problem! Since it isn't turn based and has to happen real time. How do I design my multiplayer system? Ive previously worked on turn based games, where I just pass the indices and mirror the events taking place locally, in the remote player as well.
But here with latency in the picture, I do not know how to proceed!
What I would like to do is, have the healer heal the player locally as well on the remote player's phone before he is attacked by the remote player or kill the player before the healer can heal his hp depending on the timestamp in which the events happen and reflect such on both the devices.
I think you are confused with some of the pun networking features.
If the healer heals your local player, so will be the remote instances of your local player. Your healing system must be networked of course, make sure you go through our basic tutorial to learn how to implement a simple health system.
https://doc.photonengine.com/en-us/pun/v2/demos-and-tutorials/pun-basics-tutorial/player-networking#health_synchronization
What will be slightly more complex than health management is the switch between your player and healer locally. for this you have two main variants.
transfer ownership between the player and healer: https://doc.photonengine.com/en-us/pun/v1/demos-and-tutorials/package-demos/ownership-transfer (this is a v1 demo but the principle still applies to v2)
have a invisible network player, and using your own logic in your game, have the player matching this invisible network player or the healer matching it.

Unity3D Speed Hack Detection

I want to know, is there any way to detect within our game against speed hack applications like game guardian?
I searched all over the internet but I didn't get a satisfying answer.
The solution is not more software on top. The solution is not being vulnerable to this from the start.
Example:
If you allow your users to send your server their coordinates on the world map ("I am the blue player and since I moved my full allowance, I'm now at 156/467"), it's trivial for somebody to write a teleport cheat. Just send different coordinates, boom teleported. Kids play.
If you only allow your users to send messages of intent to your server ("I am the blue player and I intend to move my full allowance in the direction of 156/467") and let your server figure out what that means (is the player allowed to move, what is his allowance, how far will the blue player get in one unit of time) and send the result back, you will never have that problem.
Do not trust client input. First rule of video game security: The client is in the hands of the enemy.
There is little point in shoving more and more software on top of your client if you keep trusting your client. Just stop and design your game so the client can send intent and the server determines outcome.
It depends on your game, but you can try detecting speed hacks by simply checking player position every frame.
Determine maximum possible move distance per second and put it in constant, so that the player wouldn't be able to change that at runtime.
Cache current position.
On next update, check the distance the player travelled and multiply it by the Time.deltaTime.
If the player travelled more than maximum allowed, the player must have cheated.
Return to step no. 2
It will be framerate independent checking as you will be using maximum allowed distance per second.
If the server has all player positions at all time and your problem is with speed hacking the solution is not that hard (assuming player position is being sent to server constantly).
Please do the following, first before updating player position check if the distance between current position and old position isn't too big
var distance = Vector3.Distance(old.position, currentposition);
if(distance<=alloweddistance)
...
and if it is avoid updating position\set player to old position\kick player. A better solution to even that, is to not allow players to send their current position to server, rather send direction they are moving and speed. The server should change their position with the direction and move speed, not client.
I found Solution for my own project that is online multiplayer and it works for any online game.
For solving this problem just we need to calculate time in server. for example we can use stopwatch and doing this in client side... and in an specific situation we can compare them over network and if there was any difference between them, (some thing like more than 3 second) then becomes clear that player is used speed hack.
I tried this method in my game and it works fine.

Unity character assets don't work in multiplayer

I'm creating an fps game and I want to have multiplayer. I use the standard character assets from unity but when I join my server with more than two players dey cant see the other player move and when they move both players move. It's like the player move script is run on both players at the same time. So if I press w on own of the clients both players move. I'm coding in c#. no error message but in the console it spams "There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene."
standard character assets from unity
player network Identity
Network Manager
You're instantiating a controlled player every time someone joins the server. If they have the same controller components, both of the objects are going to react to controls. Create a non-controllable player prefab and have it react to messages sent from the other clients.
If you have a movement component separate from your controller component, you can reuse the movement component on the non-controllable object.

Smoothing Movement In Lower-Level Networking for Unity

So I'm in the process of writing a networked game for Unity and we have to work with the lower level network API for Unity. In other words, we can only use NetworkServer, NetworkClient, and NetworkIdentity.
Forbidden ones are NetworkTransform and NetworkManager.
So we have all of our networking up and running. Everything is working fine. Basic overview of how our networking for movement is...
Client presses key and sends input to move
Packet is created and sent under the Opcode.MOVEMENT
Server receives and processes movement. We have no mitigation (we had bucket synchronization but disabled it because it was giving issues). In the update method we continuously send the position of all the players out to each other.
All clients continuously receive positions of other players and update it accordingly.
Observed effects: On the server side things are pristine. Movement is extremely smooth. On the client end there is a noticeable framerate loss. Things aren't super smooth, but the player moves without jitter. Overall: no jitter, but not smooth either.
What would be the best way to smoothing out movement on clients?

Categories