Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////NETWORKMOVEMENT SCRIPT/////
- using UnityEngine;
- using System.Collections;
- using UnityEngine.Networking;
- using System.Collections.Generic;
- [NetworkSettings(channel = 1, sendInterval = 0.05f)]
- public class NetworkMovement : NetworkBehaviour
- {
- public int moveDelay = 15;
- public bool server = false;
- [System.NonSerialized]
- public int currentDelay = 0;
- public struct Inputs
- {
- public bool N;
- public bool S;
- public bool E;
- public bool W;
- }
- //Used by server to collect results of Move() before sending to client
- public struct Results
- {
- public Vector3 position;
- }
- //Synced from server to all clients
- // [SyncVar(hook = "RecieveResults")]
- // private Results syncResults;
- private Inputs inputStruct;
- // private List<Inputs> inputsList = new List<Inputs>();
- // Use this for initialization
- void Start()
- {
- Debug.Log("NetworkMovement exists!");
- }
- void Update()
- {
- if (isLocalPlayer && !server)
- {
- //Getting clients inputs
- GetInputs(ref inputStruct);
- }
- }
- void FixedUpdate()
- {
- if (hasAuthority && !server)
- {
- Debug.Log("hasAuthority returned true in Update() of NetworkMovement, moving...");
- //Struct need to be fully new to sync, hence the temporary results struct
- Results tempResults;
- tempResults.position = Move(inputStruct);
- //syncResults = tempResults;
- }
- }
- public virtual void GetInputs(ref Inputs inputs)
- {
- inputs.N = Input.GetKey(KeyCode.W);
- inputs.S = Input.GetKey(KeyCode.S);
- inputs.E = Input.GetKey(KeyCode.D);
- inputs.W = Input.GetKey(KeyCode.A);
- Debug.Log("Inputs: " + inputs.N + inputs.S + inputs.E + inputs.W);
- }
- public virtual Vector3 Move(Inputs inputs)
- {
- Debug.Log("Move has been called.");
- if (currentDelay < 1)
- {
- Vector3 movement = Vector3.zero;
- if (inputs.N && !inputs.S)
- {
- movement = transform.up;
- }
- else if (!inputs.N && inputs.S)
- {
- movement = -(transform.up);
- }
- else if (inputs.E && !inputs.W)
- {
- movement = transform.right;
- }
- else if (!inputs.E && inputs.W)
- {
- movement = -(transform.right);
- }
- transform.Translate(movement);
- }
- else
- {
- currentDelay--;
- }
- return transform.position;
- }
- }
- ///// PLAYERMOVEMENT SCRIPT (NETWORKPAWN RENAMED) /////
- using UnityEngine;
- using System.Collections;
- public class PlayerMovement : NetworkMovement {
- public Transform playerTransform;
- public override Vector3 Move(Inputs inputs)
- {
- if (currentDelay < 1)
- {
- Vector3 movement = Vector3.zero;
- if (inputs.N && !inputs.S)
- {
- movement = transform.up;
- }
- else if (!inputs.N && inputs.S)
- {
- movement = -(transform.up);
- }
- else if (inputs.E && !inputs.W)
- {
- movement = transform.right;
- }
- else if (!inputs.E && inputs.W)
- {
- movement = -(transform.right);
- }
- playerTransform.Translate(movement);
- }
- else
- {
- currentDelay--;
- }
- return playerTransform.position;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement