PigStyle
PigStyle

Reputation: 11

How to do player movement calculations on c# console from unity movement

Question: How do i go about doing a equation on a c# console for where the player should be based on player input from unity.

To explain farther, the client is ran in unity and the server is a c# console application. When the player presses a movement key ex. (w, witch moves the player forward based on local position, as to always make the player move forward) it sends to the server that player pressed w, and then the sever responds and tells the client that it pressed w, changing the player w is pressed bool to true. Then the character moves.

Essentially I am trying to figure out how to make a equation that will do what unity does, but without a transform, or even how the transform works so that i can apply it to the server side.

Here is the client side code:

 private void Update()
{
    SendKeysToServer();
    PlayerController();
    PlayerPositionUpdateChecker();
}

public void PlayerController()
{
    if (gameObject.name == ntwmng.Playername)
    {
        MovementControllerPlayer();
        JumpControllerPlayer();
        RotationControllerPlayer();
        MovementFinalizer(); 
    }
    else
    {
        MovementControllerClones();
        JumpControllerClones();
        RotationControllerClones();
        MovementFinalizerClones();
    }
}

public void MovementControllerPlayer()
{
    if (CharContr.isGrounded)
    {
        moveDirection = new Vector3(0, 0, 0);

        if (wKeyDownPlayer)
        {
            moveDirection = moveDirection + new Vector3(0, 0, speed * Time.deltaTime);
        }
        if (sKeyDownPlayer)
        {
            moveDirection = moveDirection + new Vector3(0, 0, -speed * Time.deltaTime);
        }
        if (qKeyDownPlayer)
        {
            moveDirection = moveDirection + new Vector3(-speed * Time.deltaTime, 0, 0);
        }
        if (eKeyDownPlayer)
        {
            moveDirection = moveDirection + new Vector3(speed * Time.deltaTime, 0, 0);
        }

        moveDirection = transform.TransformDirection(moveDirection);
        //moveDirection *= speed; 
    }

}

public void RotationControllerPlayer()
{
    if (dKeyDownPlayer)
    {
        transform.Rotate(0, RotationSpeed * Time.deltaTime, 0);
    }
    if(aKeyDownPlayer)
    {
        transform.Rotate(0, -RotationSpeed * Time.deltaTime, 0);
    }
}

public void JumpControllerPlayer()
{
    if (CharContr.isGrounded)
    {

        if (SpaceKeyDownPlayer)
        {
            moveDirection.y = jumpSpeed * Time.deltaTime;
        } 
    }
}

public void MovementFinalizer()
{
    moveDirection.y -= gravity * Time.deltaTime;
    CharContr.Move(moveDirection);
}

public void CameraAndListenerController()
{
    if (gameObject.name == ntwmng.Playername)
    {
        if (playercam.enabled == false)
        {
            playercam.enabled = true;
        }
        if (AudioListener.enabled == false)
        {
            AudioListener.enabled = true;
        }
    }
    else
    {
        if (playercam.enabled == true)
        {
            playercam.enabled = false;
        }
        if (AudioListener.enabled == true)
        {
            AudioListener.enabled = false;
        }
    }
}

public void SendKeysToServer()
{
    NetworkStream NtwrkStrm = ServerConnection.GetStream();
    IFormatter Formatter = new BinaryFormatter();

    foreach (KeyCode kcode in Enum.GetValues(typeof(KeyCode)))
    {
        if(Input.GetKeyDown(kcode))
        {
            string type = "Movement Update Key Down";
            string kycode = Convert.ToString(kcode);
            //Debug.Log(kycode);
            Formatter.Serialize(NtwrkStrm, type);
            NtwrkStrm.Flush();
            Formatter.Serialize(NtwrkStrm, ntwmng.Playername);
            NtwrkStrm.Flush();
            Formatter.Serialize(NtwrkStrm, kycode);
            NtwrkStrm.Flush();
        }else
        if(Input.GetKeyUp(kcode))
        {
            string type = "Movement Update Key Up";
            string kycode = Convert.ToString(kcode);
            //Debug.Log(kycode);
            Formatter.Serialize(NtwrkStrm, type);
            NtwrkStrm.Flush();
            Formatter.Serialize(NtwrkStrm, ntwmng.Playername);
            NtwrkStrm.Flush();
            Formatter.Serialize(NtwrkStrm, kycode);
            NtwrkStrm.Flush();
        }
    }
}

public void MovementControllerClones()
{
    if (CharContr.isGrounded)
    {
        moveDirection = new Vector3(0, 0, 0);

        if (wKeyDownClone)
        {
            moveDirection = moveDirection + new Vector3(0, 0, speed * Time.deltaTime);
        }
        if (sKeyDownClone)
        {
            moveDirection = moveDirection + new Vector3(0, 0, -speed * Time.deltaTime);
        }
        if (qKeyDownClone)
        {
            moveDirection = moveDirection + new Vector3(-speed * Time.deltaTime, 0, 0);
        }
        if (eKeyDownClone)
        {
            moveDirection = moveDirection + new Vector3(speed * Time.deltaTime, 0, 0);
        }

        moveDirection = transform.TransformDirection(moveDirection);
        //moveDirection *= speed;
    }

}

public void RotationControllerClones()
{
    if (dKeyDownClone)
    {
        transform.Rotate(0, RotationSpeed * Time.deltaTime, 0);
    }
    if (aKeyDownClone)
    {
        transform.Rotate(0, -RotationSpeed * Time.deltaTime, 0);
    }
}

public void JumpControllerClones()
{
    if (CharContr.isGrounded)
    {

        if (SpaceKeyDownClone)
        {
            moveDirection.y = jumpSpeed* Time.deltaTime;
        }
    }
}

public void MovementFinalizerClones()
{
    moveDirection.y -= gravity * Time.deltaTime;
    CharContr.Move(moveDirection);
}

public void CloneKeyUpdater(string type,string kcode)
{
    //Debug.Log("Key " + kcode + " Clone Updating");
    if (type == "Movement Update Key Down")
    {
        if (kcode == "W")
        {
            wKeyDownClone = true;
        }
        if (kcode == "A")
        {
            aKeyDownClone = true;
        }
        if (kcode == "D")
        {
            dKeyDownClone = true;
        }
        if (kcode == "S")
        {
            sKeyDownClone = true;
        }
        if (kcode == "Q")
        {
            qKeyDownClone = true;
        }
        if (kcode == "E")
        {
            eKeyDownClone = true;
        }
        if (kcode == "Space")
        {
            SpaceKeyDownClone = true;
        }
    }
    else if (type == "Movement Update Key Up")
    {
        if (kcode == "W")
        {
            wKeyDownClone = false;
        }
        if (kcode == "A")
        {
            aKeyDownClone = false;
        }
        if (kcode == "D")
        {
            dKeyDownClone = false;
        }
        if (kcode == "S")
        {
            sKeyDownClone = false;
        }
        if (kcode == "Q")
        {
            qKeyDownClone = false;
        }
        if (kcode == "E")
        {
            eKeyDownClone = false;
        }
        if (kcode == "Space")
        {
            SpaceKeyDownClone = false;
        }
    }
}

public void PlayerKeyUpdater(string type, string kcode)
{
    //Debug.Log("Key down " + kcode + " Updating");
    if (type == "Movement Update Key Down")
    {
        if (kcode == "W")
        {
            wKeyDownPlayer = true;
        }
        if (kcode == "A")
        {
            aKeyDownPlayer = true;
        }
        if (kcode == "D")
        {
            dKeyDownPlayer = true;
        }
        if (kcode == "S")
        {
            sKeyDownPlayer = true;
        }
        if (kcode == "Q")
        {
            qKeyDownPlayer = true;
        }
        if (kcode == "E")
        {
            eKeyDownPlayer = true;
        }
        if (kcode == "Space")
        {
            SpaceKeyDownPlayer = true;
        }
    }
    else if (type == "Movement Update Key Up")
    {
        if (kcode == "W")
        {
            wKeyDownPlayer = false;
        }
        if (kcode == "A")
        {
            aKeyDownPlayer = false;
        }
        if (kcode == "D")
        {
            dKeyDownPlayer = false;
        }
        if (kcode == "S")
        {
            sKeyDownPlayer = false;
        }
        if (kcode == "Q")
        {
            qKeyDownPlayer = false;
        }
        if (kcode == "E")
        {
            eKeyDownPlayer = false;
        }
        if (kcode == "Space")
        {
            SpaceKeyDownPlayer = false;
        }
    }
}

Any help will be muchly appreciated. :)

Edit: so i have figured out a equation that i am using to try and mimic player movement, as of right now it only work between 0 and 90 degrees, just tell i get it exact. the equation comes out really close, but is just a tiny bit off. Any suggestions? Also, this is running on client side, no server influence, so that I can get the math to an exact. The math comes out about 1 unit per second slower then the players current position. Basically after pressing w for 1 second with speed = 20 the math comes out to 19 and actual position is 20.

here is code

    if (CharContr.velocity != Vector3.zero)//checks if the player is moving
    {
        if (rotation < 90f && rotation > 0f) // makes sure the rotation is 0-90
        {
            percent = (100 / (90 / rotation)) / 100; // gets the percent
            x = x + (percent * speed * Time.deltaTime); //adds to current position multiplied by speed and delta time
            invertPercent = 1 - percent; //gets the percent for z
            z = z + (invertPercent * speed * Time.deltaTime); // adds and stuff again
        }

Upvotes: 1

Views: 973

Answers (3)

PigStyle
PigStyle

Reputation: 11

so i did some searching and have come up with a solution, this is only for if the player is moving forward and the player direction is between 0 and 90. basically i have to use transform.translate instead of using a character controller. the reason i am doing this is so that when the player presses w it will send it to the server and the server can run this at the same time, then use the info to be able to tell were players should be.

if (rotation < 90f && rotation > 0f)
        {
            moveDirection = new Vector3(0, 0, 0);
            percent = (100 / (90 / rotation)) / 100;
            invertPercent = 1 - percent;

            x = percent * speed;
            z = invertPercent * speed;
            transform.Translate(x * Time.deltaTime, 0, z * Time.deltaTime, Space.World);
            newposition = newposition + new Vector3(x * Time.deltaTime, 0, z * Time.deltaTime);
        }

Upvotes: 0

Programmer
Programmer

Reputation: 125455

This is really complicated than you thought if you want to implement it yourself instead of using Unity's built-in NetworkTransform. Read this "Client-Side Prediction and Server Reconciliation" article. There are many parts of that article but it shows you what's going on and how this is done.

Although, Unity simplified a part of this if you are using Rigidbody by providing a new function (Physics.Simulate). You simulate all the Rigidbody on the server with the Physics.Simulate function, serialize the transform of each one and send to all the clients.

On the clients, receive the data, de-serialize them then lerp from the current transform to the received ones. You can find an example of how to use Physics.Simulate to determine the position of a Rigidbody in x second here.

Upvotes: 1

Endrik
Endrik

Reputation: 606

Sending keycode to server and syncronize position based on bool is not best practice.

The good practice would be, sending 3 float value of x, y, and z position to the server, then broadcast it to other player. so other player has the exact position without approximation

Sorry if i answer in wrong place, because i don't have enough rep to make comment.

Upvotes: 0

Related Questions