Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.Networking;
- [NetworkSettings(channel = 0, sendInterval = 1 / 30f)]
- public class networkPlayer : NetworkBehaviour
- {
- [Range(0.0f, 20.0f)]
- public float lerpAmount = 1;
- [SyncVar]
- Vector2 syncPosition;
- [SyncVar]
- Quaternion syncRotaiton;
- [SyncVar]
- Vector2 syncVelocity;
- GameObject playerObject;
- Transform playerTransform;
- void Start()
- {
- playerObject = gameObject;
- playerTransform = GetComponent<Transform>();
- }
- void Update()
- {
- LerpValues();
- }
- void FixedUpdate()
- {
- if (isLocalPlayer)
- CmdUpdatePosition(playerTransform.position, playerTransform.rotation, playerObject.GetComponent<Rigidbody2D>().velocity);
- }
- void LerpValues()
- {
- if (!isLocalPlayer)
- {
- playerObject.GetComponent<Rigidbody2D>().velocity = syncVelocity;
- playerTransform.rotation = syncRotaiton;
- playerTransform.position = Vector3.Lerp(playerTransform.position, syncPosition, Time.deltaTime * lerpAmount);
- }
- }
- [Command]
- void CmdUpdatePosition(Vector2 position, Quaternion rotation, Vector2 velocity)
- {
- syncPosition = position;
- syncRotaiton = rotation;
- syncVelocity = velocity;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement