Beispiel #1
0
 /// <summary>Sends player input to the server.</summary>
 /// <param name="_inputs"></param>
 public static void PlayerMovement(PlayerController.InputPack _pack)
 {
     using (Packet _packet = new Packet((int)ClientPackets.playerMovement))
     {
         _packet.Write(_pack.input);
         _packet.Write(_pack.camRotation);
         _packet.Write(_pack.orientation);
         _packet.Write(_pack.tick);
         SendUDPData(_packet);
     }
 }
Beispiel #2
0
    public void Movement(PlayerController.InputPack _pack)
    {
        inputs = PlayerController.DecodeByte(_pack.input);
        ResolveInput(inputs);
        orientation.rotation = _pack.orientation;
        playerCam.rotation   = _pack.camRotation;

        //Extra gravity
        rb.AddForce(Vector3.down * Time.deltaTime * 10);

        //Find actual velocity relative to where player is looking
        Vector2 mag = FindVelRelativeToLook();
        float   xMag = mag.x, yMag = mag.y;

        //Counteract sliding and sloppy movement
        CounterMovement(x, y, mag);

        //If holding jump && ready to jump, then jump
        if (readyToJump && jumping)
        {
            Jump();
        }

        //Set max speed
        float maxSpeed = this.maxSpeed;

        //If sliding down a ramp, add force down so player stays grounded and also builds speed
        if (crouching && grounded && readyToJump)
        {
            rb.AddForce(Vector3.down * Time.deltaTime * 3000);
            return;
        }

        //If speed is larger than maxspeed, cancel out the input so you don't go over max speed
        if (x > 0 && xMag > maxSpeed)
        {
            x = 0;
        }
        if (x < 0 && xMag < -maxSpeed)
        {
            x = 0;
        }
        if (y > 0 && yMag > maxSpeed)
        {
            y = 0;
        }
        if (y < 0 && yMag < -maxSpeed)
        {
            y = 0;
        }

        //Some multipliers
        float multiplier = 1f, multiplierV = 1f;

        // Movement in air
        if (!grounded)
        {
            multiplier  = 0.5f;
            multiplierV = 0.5f;
        }

        // Movement while sliding
        //if (grounded && crouching) { multiplierV = 0.5f; multiplier = 0.5f; }

        //Apply forces to move player
        rb.AddForce(orientation.transform.forward * y * moveSpeed * Time.deltaTime * multiplier * multiplierV);
        rb.AddForce(orientation.transform.right * x * moveSpeed * Time.deltaTime * multiplier);
    }