Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CharacterManager : MonoBehaviour {
- public float Speed = 5f;
- public float JumpHeight = 2f;
- public float GroundDistance = 0.2f;
- public float sprintSpeed = 7.5f;
- public float mouseSensitivity = 5f;
- public float upDownRange = 85.0f;
- public float MouseSensitivity = 5.0f;
- float verticalRotation = 0f;
- public LayerMask Ground;
- private Rigidbody _body;
- private Vector3 _inputs = Vector3.zero;
- private bool _isGrounded = true;
- private Transform _groundChecker;
- private Camera cam;
- private PhotonView mine;
- private bool isMine = false;
- Vector3 oldPosition;
- Vector3 networkPosition;
- Quaternion networkRotation;
- [Header("Network Sync Settings")]
- public float moveSpeed = 25f;
- public float rotationSpeed = 300f;
- void Awake()
- {
- mine = GetComponent<PhotonView>();
- _body = GetComponent<Rigidbody>();
- _groundChecker = transform.GetChild(0);
- if(!mine.isMine)
- {
- isMine = false;
- return;
- }
- isMine = true;
- cam = GetComponentInChildren<Camera>();
- cam.gameObject.SetActive(true);
- }
- void Update()
- {
- if(isMine)
- {
- _isGrounded = Physics.CheckSphere(_groundChecker.position, GroundDistance, Ground, QueryTriggerInteraction.Ignore);
- float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
- transform.Rotate(0, rotLeftRight, 0);
- verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
- verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
- cam.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
- _inputs = Vector3.zero;
- _inputs.x = Input.GetAxis("Horizontal");
- _inputs.z = Input.GetAxis("Vertical");
- forwardBackward = _inputs.x;
- leftRight = _inputs.z;
- jump = Input.GetButtonDown("Jump");
- sprinting = Input.GetKey(KeyCode.LeftShift);
- if (Input.GetButtonDown("Jump") && _isGrounded)
- {
- _body.AddForce(Vector3.up * Mathf.Sqrt(JumpHeight * -2f * Physics.gravity.y), ForceMode.VelocityChange);
- }
- if (Input.GetKey(KeyCode.LeftShift) && _inputs.z > 0)
- {
- _inputs.z = _inputs.z * sprintSpeed;
- }
- }
- else
- {
- transform.position = Vector3.MoveTowards(transform.position, networkPosition, Time.fixedDeltaTime * moveSpeed);
- transform.rotation = Quaternion.RotateTowards(transform.rotation, networkRotation, Time.fixedDeltaTime * rotationSpeed);
- }
- }
- private int updatesPassed = 0;
- private float PlayerUpdate(bool jump, bool sprint, float forwards, float sideways)
- {
- _isGrounded = Physics.CheckSphere(_groundChecker.position, GroundDistance, Ground, QueryTriggerInteraction.Ignore);
- _inputs = Vector3.zero;
- _inputs.x = forwards;
- _inputs.z = sideways;
- float f = 0;
- if (jump && _isGrounded)
- {
- jump = false;
- f = Mathf.Sqrt(JumpHeight * -2f * Physics.gravity.y);
- _body.AddForce(Vector3.up * Mathf.Sqrt(JumpHeight * -2f * Physics.gravity.y), ForceMode.VelocityChange);
- }
- if (sprint && _inputs.z > 0)
- {
- _inputs.z = _inputs.z * sprintSpeed;
- }
- return f;
- }
- private Vector3 SimulateUpdate(Vector3 inp, float force)
- {
- _body.MovePosition(_body.position + networkRotation * inp * Speed * (Time.fixedDeltaTime * updatesPassed));
- return transform.position + Vector3.up * force * (Time.fixedDeltaTime * updatesPassed);
- }
- void FixedUpdate()
- {
- if(teleport == false)
- {
- _body.MovePosition(_body.position + transform.rotation * _inputs * Speed * Time.fixedDeltaTime);
- }
- else
- {
- teleport = false;
- _body.position = networkPosition;
- _body.rotation = networkRotation;
- }
- updatesPassed++;
- }
- public float forwardBackward = 0;
- public float leftRight = 0;
- public bool jump = false;
- public bool sprinting = false;
- public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
- {
- if(stream.isWriting)
- {
- stream.SendNext(forwardBackward);
- stream.SendNext(leftRight);
- stream.SendNext(jump);
- stream.SendNext(sprinting);
- stream.SendNext(transform.position);
- stream.SendNext(transform.rotation);
- Vector3 movement = transform.position - oldPosition;
- stream.SendNext(movement);
- oldPosition = transform.position;
- }
- else
- {
- float forwardBackward = (float)stream.ReceiveNext();
- float leftRight = (float)stream.ReceiveNext();
- bool jump = (bool)stream.ReceiveNext();
- bool sprinting = (bool)stream.ReceiveNext();
- networkPosition = (Vector3)stream.ReceiveNext();
- networkRotation = (Quaternion)stream.ReceiveNext();
- Vector3 dirM = (Vector3)stream.ReceiveNext();
- if(PhotonNetwork.isMasterClient)
- {
- float force = PlayerUpdate(jump, sprinting, forwardBackward, leftRight);
- float lagC = Mathf.Abs((float) (PhotonNetwork.time - info.timestamp));
- networkPosition += (dirM * lagC);
- Vector3 pos = transform.position;
- SimulateUpdate(new Vector3(forwardBackward, 0, leftRight), force);
- updatesPassed = 0;
- float dist = Vector3.Distance(networkPosition, transform.position);
- transform.position = pos;
- if(dist > 0.25f)
- {
- GetComponent<PhotonView>().RPC("SetPosition", PhotonTargets.All, transform.position, transform.rotation);
- }
- }
- else
- {
- float lag = Mathf.Abs((float) (PhotonNetwork.time - info.timestamp));
- networkPosition += (dirM * lag);
- }
- }
- }
- private bool teleport = false;
- [PunRPC]
- public void SetPosition(Vector3 position, Quaternion rotation)
- {
- transform.position = position;
- transform.rotation = rotation;
- networkPosition = position;
- networkRotation = rotation;
- teleport = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement