Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Mirror;
- using System.Timers;
- public class PlayerControler : NetworkBehaviour
- {
- [Header("Player Move Settings")]
- public float Speed = 5f;
- public float MaximumVelocity = 2f;
- public float JumpForce = 3f;
- private bool isJump;
- private Rigidbody rig;
- private GroundCheck GroundCheck;
- private Vector3 velocity;
- private Vector2 input;
- private Vector3 serverPos;
- private SteamNetworkManager networkManager;
- private void OnEnable()
- {
- GroundCheck = GameObject.FindGameObjectWithTag("Player").GetComponent<GroundCheck>();
- rig = GetComponent<Rigidbody>();
- }
- private void Awake()
- {
- networkManager = SteamNetworkManager.instance;
- }
- private void FixedUpdate()
- {
- print("Client: " + netId);
- if(isLocalPlayer && isServer)
- {
- GetInput();
- }
- if (isServer)
- {
- MovePlayer();
- }
- CmdJump(false);
- }
- private void MovePlayer()
- {
- if (isJump)
- {
- rig.AddForce(new Vector3(0, JumpForce * 80, 0), ForceMode.Acceleration);
- }
- float speedToDelta = Speed * Time.fixedDeltaTime * 2;
- float yVelocity = rig.velocity.y;
- rig.velocity = new Vector3(speedToDelta * input.x, 0, speedToDelta * input.y).normalized * MaximumVelocity;
- Vector3 v = rig.velocity;
- v.y = yVelocity;
- rig.velocity = v;
- }
- [Command]
- private void CmdReadInput(float x,float z)
- {
- input.x = x;
- input.y = z;
- }
- [Command]
- private void CmdJump(bool jump)
- {
- isJump = jump;
- }
- private void SyncClientPosition(object sender, ElapsedEventArgs e)
- {
- UnityMainThreadDispatcher.Instance().Enqueue(() => CmdSyncClientPosition(transform.position));
- }
- private void SyncClientRotation(object sender, ElapsedEventArgs e)
- {
- UnityMainThreadDispatcher.Instance().Enqueue(() => CmdSyncClientRotation(transform.eulerAngles));
- }
- [Command]
- private void CmdSyncClientPosition(Vector3 serverPosition)
- {
- TargetSyncToServerPosition(serverPosition);
- }
- [TargetRpc]
- private void TargetSyncToServerPosition(Vector3 serverPosition)
- {
- serverPos = serverPosition;
- }
- [Command]
- private void CmdSyncClientRotation(Vector3 clientRotation)
- {
- transform.eulerAngles = clientRotation;
- }
- private void GetInput()
- {
- if (Input.GetButtonDown("Jump") && GroundCheck.IsOnGround == true)
- {
- CmdJump(true);
- }
- else
- {
- CmdJump(false);
- }
- float horizontal = Input.GetAxis("Horizontal");
- float vertical = Input.GetAxis("Vertical");
- input = new Vector2(horizontal, vertical);
- if (input.sqrMagnitude > 1)
- {
- input.Normalize();
- }
- CmdReadInput(input.x,input.y);
- MovePlayer();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement