Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I make a multiplayer FPS Prediction System

Discussion in 'Scripting' started by 420BlazeIt, Dec 27, 2014.

  1. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    I have seen quill18creates's video on this topic :

    BUT
    My project isn't using Photon nor is it using C# so quill18create's video didn't really help me at all.
    Can someone send me a link to a Javascript tutorial for this prediction system without photon please.
    Thank you.
     
  2. StickyKeyStudios

    StickyKeyStudios

    Joined:
    Jun 23, 2014
    Posts:
    33
    Are you using the standard Unity networking?
     
  3. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Yes.
     
  4. StickyKeyStudios

    StickyKeyStudios

    Joined:
    Jun 23, 2014
    Posts:
    33
    Sorry for the late reply. I followed the same tutorial in my game, and this is what I did. I can't say 100% this works because I removed some of the code pertaining to synced animations since I haven't fulled fixed that yet.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(CharacterController))]
    5. [RequireComponent(typeof(NetworkView))]
    6.  
    7. public class FirstPersonController : MonoBehaviour {
    8.  
    9.     public float defaultFieldOfView;
    10.     public float walkSpeed;
    11.     public float sprintSpeed;
    12.     public float mouseSensitivityX;
    13.     public float mouseSensitivityY;
    14.     public float headTiltMinimum;
    15.     public float headTiltMaximum;
    16.     public float jumpHeight;
    17.     public float staminaEnergyUsage;
    18.     public float staminaEnergyGain;
    19.     public float staminaCapacity;
    20.     public float staminaGainDelay;
    21.     public float staminaUseFOV;
    22.     public float maxHealth;
    23.     public float currentHealth;
    24.  
    25.     private float rotUpDown = 0;
    26.     private float verticalVelocity = 0;
    27.     private bool canUseStamina = true;
    28.     private float staminaTime = 0;
    29.     private CharacterController cc;
    30.     private bool screenLock = true;
    31.  
    32.     // Network Sync
    33.     private float lastSynchronizationTime = 0f;
    34.     private float syncDelay = 0f;
    35.     private float syncTime = 0f;
    36.     private Vector3 syncStartPosition = Vector3.zero;
    37.     private Vector3 syncEndPosition = Vector3.zero;
    38.     private    Quaternion syncRotStart = Quaternion.identity;
    39.     private Quaternion syncRotEnd = Quaternion.identity;
    40.  
    41.     void Start () {
    42.         Screen.lockCursor = true;
    43.         cc = GetComponent<CharacterController>();
    44.     }
    45.  
    46.     void OnGUI()
    47.     {
    48.         GUI.Box (new Rect (32, (Screen.height - 48), (Screen.width / 4) - 40, 32), "");
    49.         GUI.Box (new Rect (32 + (Screen.width / 4) - 8, (Screen.height - 48), (Screen.width / 4) - 40, 32), "");
    50.         GUI.Box (new Rect (32 + (Screen.width / 2) - 16, (Screen.height - 48), (Screen.width / 4) - 40, 32), "");
    51.         GUI.Box (new Rect (32 + (3 * Screen.width / 4) - 24, (Screen.height - 48), (Screen.width / 4) - 40, 32), "");
    52.     }
    53.  
    54.     public void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
    55.     {
    56.         Vector3 syncPosition = Vector3.zero;
    57.         Quaternion syncRotation = Quaternion.identity;
    58.         Vector3 syncVelocity = Vector3.zero;
    59.  
    60.         if (stream.isWriting)
    61.         {
    62.             syncPosition = transform.position;
    63.             stream.Serialize(ref syncPosition);
    64.  
    65.             syncRotation = transform.rotation;
    66.             stream.Serialize(ref syncRotation);
    67.  
    68.             syncVelocity = GetComponent<CharacterController>().velocity;
    69.             stream.Serialize(ref syncVelocity);
    70.         }
    71.         else
    72.         {
    73.             stream.Serialize(ref syncPosition);
    74.             stream.Serialize(ref syncRotation);
    75.             stream.Serialize(ref syncVelocity);
    76.  
    77.             syncTime = 0f;
    78.             syncDelay = Time.time - lastSynchronizationTime;
    79.             lastSynchronizationTime = Time.time;
    80.          
    81.             syncEndPosition = syncPosition + syncVelocity * syncDelay;
    82.             syncStartPosition = transform.position;
    83.  
    84.             syncRotEnd = syncRotation;
    85.             syncRotStart = transform.rotation;
    86.         }
    87.     }
    88.     public void SyncedMovement()
    89.     {
    90.         syncTime += Time.deltaTime;
    91.         transform.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
    92.         transform.rotation = Quaternion.Lerp (syncRotStart, syncRotEnd, syncTime / syncDelay);
    93.     }
    94.  
    95.     void OnApplicationFocus(bool focusStatus)
    96.     {
    97.         if (focusStatus)
    98.         {
    99.             if (screenLock)
    100.             {
    101.                 Screen.lockCursor = true;
    102.             }
    103.         }
    104.     }
    105.  
    106.     void Update () {
    107.         if (networkView.isMine)
    108.         {
    109.             UpdateMovement();
    110.         }
    111.         else
    112.         {
    113.             SyncedMovement();
    114.         }
    115.     }
    116.  
    117.     private void UpdateMovement()
    118.     {
    119.         // Rotation
    120.         float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivityX;
    121.         transform.Rotate(0, rotLeftRight, 0);
    122.      
    123.         rotUpDown -= Input.GetAxis("Mouse Y") * mouseSensitivityY;
    124.         rotUpDown = Mathf.Clamp (rotUpDown, -headTiltMinimum, headTiltMaximum);
    125.      
    126.         Camera.main.transform.localRotation = Quaternion.Euler (rotUpDown, 0, 0);
    127.      
    128.         // Movement and Jumping
    129.         float desiredSpeed = walkSpeed;
    130.  
    131.         if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d"))
    132.         {
    133.             walk = true;
    134.         }
    135.         else
    136.         {
    137.             walk = false;
    138.         }
    139.  
    140.         if (Input.GetButton("Sprint") && staminaCapacity > 0 && canUseStamina) // Sprint
    141.         {
    142.             sprint = true;
    143.             desiredSpeed = sprintSpeed;
    144.             staminaCapacity -= staminaEnergyUsage/2;
    145.             if (staminaCapacity <= 0)
    146.             {
    147.                 staminaTime = Time.time;
    148.             }
    149.         }
    150.         else
    151.         {
    152.             sprint = false;
    153.             if (staminaCapacity < 100 && ((Time.time - staminaTime) >= staminaGainDelay))
    154.             {
    155.                 if ((staminaCapacity += staminaEnergyGain) > 100)
    156.                 {
    157.                     staminaCapacity = 100;
    158.                 }
    159.                 else
    160.                 {
    161.                     staminaCapacity += staminaEnergyGain;
    162.                 }
    163.             }
    164.         }
    165.  
    166.         if (sprint)
    167.         {
    168.             Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, staminaUseFOV, Time.deltaTime * 10);
    169.         }
    170.         else
    171.         {
    172.             Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, defaultFieldOfView, Time.deltaTime * 10);
    173.         }
    174.      
    175.         float forwardSpeed = Input.GetAxis("Vertical") * desiredSpeed;
    176.         float sideSpeed = Input.GetAxis("Horizontal") * desiredSpeed;
    177.      
    178.         verticalVelocity += Physics.gravity.y * Time.deltaTime;
    179.      
    180.         if (Input.GetButtonDown("Jump") && cc.isGrounded)
    181.         {
    182.             verticalVelocity = jumpHeight;
    183.         }
    184.      
    185.         Vector3 speed = new Vector3(sideSpeed, verticalVelocity, forwardSpeed);
    186.      
    187.         speed = transform.rotation * speed;
    188.      
    189.         cc.Move(speed * Time.deltaTime);
    190.     }
    191. }
    192.  
    EDIT: My mistakes. You wanted JavaScript not C#. There's also on OnGUI in there that you can remove if you still plan on using this.
     
  5. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    I appreciate the help. I will delete the OnGUI and see how the script goes.
     
  6. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102


    Assets/Scripts/FirstPersonController.cs(125,25): error CS0103: The name `walk' does not exist in the current context
    Assets/Scripts/FirstPersonController.cs(129,25): error CS0103: The name `walk' does not exist in the current context
    Assets/Scripts/FirstPersonController.cs(134,25): error CS0103: The name `sprint' does not exist in the current context
    Assets/Scripts/FirstPersonController.cs(144,25): error CS0103: The name `sprint' does not exist in the current context
    Assets/Scripts/FirstPersonController.cs(158,21): error CS0103: The name `sprint' does not exist in the current context
     
  7. StickyKeyStudios

    StickyKeyStudios

    Joined:
    Jun 23, 2014
    Posts:
    33

    Just remove those, they were part of the animation networking I was working on.
     
  8. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Silly me :)
     
  9. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Could you please cut out the unrelated parts of this script I don't really know anything about C# so could you please just send me the Prediction System part of the script please. Also what is the file name supposed to be? And do I attach this script to the Player Prefab?