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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[FORGE NETWORKING] Authoritative Movement

Discussion in 'Multiplayer' started by Xazerek, May 16, 2016.

  1. Xazerek

    Xazerek

    Joined:
    Nov 3, 2014
    Posts:
    134
    Hello! Can u tell me how can i make alternative authoritative movement in one script?

    I can't do it like in video with authoritative input cause my controlling script is in Vector3.

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5. using BeardedManStudios.Network;
    6.  
    7. public class MultiplayerController : NetworkedMonoBehavior {
    8.  
    9.     [Space(5)]
    10.     [Header("OffCore")]
    11.    
    12.     public GameObject camera;
    13.     public MultiplayerController script;
    14.    
    15.    
    16.     Animator anim;
    17.    
    18.     [NetSync]
    19.     public float x;
    20.    
    21.     [NetSync]
    22.     public float y;
    23.    
    24.     [NetSync]
    25.     public bool walking;
    26.  
    27.     private GFRectGrid grid;
    28.     public float roamingTime = 1.0f; //how long it takes to move from one tile to another
    29.    
    30.     //whether the object is to move or not, to where and how fast
    31.     private bool doMove = false;
    32.     private Vector3 goal;
    33.     private float roamingSpeed;
    34.    
    35.     //cache the transform for performance
    36.     private Transform cachedTransform;
    37.  
    38.     void Start(){
    39.         if (!IsOwner)
    40.         {
    41.             camera.SetActive(false);
    42.         }
    43.         else
    44.         {
    45.  
    46.         }
    47.        
    48.        
    49.         anim = GetComponent<Animator>();
    50.        
    51.         cachedTransform = transform;
    52.         grid = ForbiddenTilesExample.movementGrid;
    53.    
    54.         //make a check to prevent getting stuck in a null exception
    55.         if(grid){
    56.             //snap to the grid  no matter where we are
    57.             grid.AlignTransform(cachedTransform);
    58.         }
    59.     }
    60.    
    61.     void Update(){
    62.         if(!grid)
    63.             return;
    64.        
    65.         if(doMove){
    66.             //move towards the desination
    67.             Vector3 newPosition = cachedTransform.position;
    68.             newPosition.x = Mathf.MoveTowards(cachedTransform.position.x, goal.x, roamingSpeed * Time.deltaTime);
    69.             newPosition.y = Mathf.MoveTowards(cachedTransform.position.y, goal.y, roamingSpeed * Time.deltaTime);
    70.             cachedTransform.position = newPosition;
    71.             //check if we reached the destination (use a certain tolerance so we don't miss the point becase of rounding errors)
    72.             if(Mathf.Abs(cachedTransform.position.x - goal.x) < 0.01f && Mathf.Abs(cachedTransform.position.y - goal.y) < 0.01f)
    73.             {
    74.                 doMove = false;
    75.                 walking = false;
    76.             }
    77.             //if we did stop moving
    78.         } else{
    79.             //make sure the time is always positive
    80.             if(roamingTime < 0.01f)
    81.                 roamingTime = 0.01f;
    82.             //find the next destination
    83.             goal = FindNextFace();
    84.             //--- let's check if the goal is allowed, if not we will pick another direction during the next frame ---
    85.             if(ForbiddenTilesExample.CheckSquare(goal)){
    86.                 //calculate speed by dividing distance (one of the two distances will be 0, we need the other one) through time
    87.                 roamingSpeed = Mathf.Max(Mathf.Abs(cachedTransform.position.x - goal.x), Mathf.Abs(cachedTransform.position.y - goal.y)) / roamingTime;
    88.                 //resume movement with the new goal
    89.                 doMove = true;
    90.             } else{
    91.                 Debug.Log("Collision detected.");
    92.                 walking = false;
    93.             }
    94.         }
    95.        
    96.         anim.SetBool("isWalking", walking);
    97.         anim.SetFloat("y", y);
    98.         anim.SetFloat("x", x);
    99.     }
    100.  
    101.     Vector3 FindNextFace(){
    102.         //we will be operating in grid space, so convert the position
    103.         Vector3 newPosition = grid.WorldToGrid(cachedTransform.position);
    104.            
    105.         if(Input.GetKey(KeyCode.LeftArrow)){
    106.             y = 0f;
    107.             x = -1f;
    108.             walking = true;
    109.             newPosition = newPosition + new Vector3(-1,0,0);
    110.         } else if(Input.GetKey(KeyCode.RightArrow)){
    111.             y = 0f;
    112.             x = 1f;
    113.             walking = true;
    114.             newPosition = newPosition + new Vector3(1,0,0);
    115.         } else if(Input.GetKey(KeyCode.DownArrow)){
    116.             x = 0f;
    117.             y = -1f;
    118.             walking = true;
    119.             newPosition = newPosition + new Vector3(0,-1,0);
    120.         } else if(Input.GetKey(KeyCode.UpArrow)){
    121.             x = 0f;
    122.             y = 1f;
    123.             walking = true;
    124.             newPosition = newPosition + new Vector3(0,1,0);
    125.         }
    126.         //if we would wander off beyond the size of the grid turn the other way around
    127.         for(int j = 0; j < 2; j++){
    128.             if(Mathf.Abs(newPosition[j]) > grid.size[j] / grid.spacing[j])
    129.                 newPosition[j] -= Mathf.Sign(newPosition[j]) * 2.0f;
    130.         }
    131.        
    132.         //return the position in world space
    133.         return grid.GridToWorld(newPosition);
    134.     }
    135.    
    136.  
    137. }
    138.  
    139.  
    Please help, i need to make this authoritative!! :C
     
  2. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    Hey there!
    Is there a reason the FindNextFace() where you input to the system cannot simply be made Authoritative?
     
  3. Xazerek

    Xazerek

    Joined:
    Nov 3, 2014
    Posts:
    134
    No idea how to do that for Vector3.

    Can u show a simple example how to do that in my script?
     
  4. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    you are kind of missunderstanding what authoritative input is. Technically could just send the Vector3 to the server and execute code based on that. But that mitigates the purpose of authoritative input.

    Authoritative input is basically where you are sending your keystrokes to the server and nothing more. The server then executes code and sends updates to you (the player) based on what happens from pressing certain keys. This ensures there is literally no way the client could cheat because the server is actually executing the code.