Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Emulating Camera.ScreenPointToRay(), without a Camera, for Server

Discussion in 'Scripting' started by zevonbiebelbrott, Aug 23, 2023.

  1. zevonbiebelbrott

    zevonbiebelbrott

    Joined:
    Feb 14, 2021
    Posts:
    97
    I am allowing cannons in my game to be aimed with the mouse, using a simple method to convert the current mouse input into a world position (the target for the cannons).

    Since its an online game, I have to send input from the Client to the Server and then call the Method to calculate the direction that the cannons should face.

    However, I only want to send from the Client to the Server the mouse Input.
    Input.mousePosition


    And then I want the server to call this method:

    void CalculateAnglesFromMouseRay()
    {
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit, 1200f))
    {
    ...
    }


    However the Server does not have a camera available, so is there a way to emulate:
    Camera.main.ScreenPointToRay()
    ?

    I know I can let the Client send something like targetPosition and then call on the Server:
    Pseudo code:
    directionToTarget =(thisClientCamera.transform.position - targetPosition).normalized;


    But then the player/client can cheat by sending all kinds of targetPositions.
    This is why I think mousePosition is much safer as it cannot go "out of bounds"(there is no pixels out of bounds lol).
    How can I fake/emulate Camera.ScreenPointToRay()?

    Btw im willing/planning to keep every clients camera position and rotation cached on the server and in fact maybe make its movement server authoritative aswell, so getting each clients camera position/rotation for a possible solution to emulate ScreenToRay wouldnt be a problem.

    Thanks men

    edit: is it authoritative or authoritive?
     
    Last edited: Aug 23, 2023
  2. zevonbiebelbrott

    zevonbiebelbrott

    Joined:
    Feb 14, 2021
    Posts:
    97
    I found this:


    Ray ScreenToWorldPoint(Vector3 screenPos) {
    // Remap so (0, 0) is the center of the window,
    // and the edges are at -0.5 and +0.5.
    Vector2 relative = new Vector2(
    screenPos.x / Screen.width - 0.5f,
    screenPos.y / Screen.height - 0.5f
    );
    // Angle in radians from the view axis
    // to the top plane of the view pyramid.
    float verticalAngle = 0.5f * Mathf.Deg2Rad * fieldOfView;

    // World space height of the view pyramid
    // measured at 1 m depth from the camera.
    float worldHeight = 2f * Mathf.Tan(verticalAngle);

    // Convert relative position to world units.
    Vector3 worldUnits = relative * worldHeight;
    worldUnits.x *= aspect;
    worldUnits.z = 1;

    // Rotate to match camera orientation.
    Vector3 direction = transform.rotation * worldUnits;

    // Output a ray from camera position, along this direction.
    return new Ray(transform.position, direction);


    from:
    https://gamedev.stackexchange.com/questions/194575/what-is-the-logic-behind-of-screenpointtoray
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Note that a client could just as well send a fake mousePosition rather than a fake targetPosition. Using mouse input is now more secure. The server would actually need to verify the client‘s data. This will actually be more difficult with a mouse position (how big is the client‘s screen? server has no authority over that) than a targetPosition (is it reasonable within the game space? did it change more than it ever could in a single tick?).
     
    zevonbiebelbrott and Bunny83 like this.
  4. zevonbiebelbrott

    zevonbiebelbrott

    Joined:
    Feb 14, 2021
    Posts:
    97
    You might be right because my current approach is too much voodoo for the problem its trying to solve. I will go back to using targetPos and will just have to prune that, hopefully I can do it without a smelly distance check

    I dont think mouseposition can be exploited as much though, because its literally clamped to the pixels of the screen, but like I said it involves too much voodoo to make it work properly
     
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    pretty sure sever side doesn't have a screen, server just has data. Where is cannon pointing? Where is the hit position of that direction? Where is CANNON!? lol..

    Just like your mouse screen position, is something your game needs to give relevant data, to make the game do what it's gotta do. So handle mouse on your side, get target world position, then send target world position. :)