Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Cant shoot correctly

Discussion in 'Multiplayer' started by Zachary_gee, Apr 17, 2017.

  1. Zachary_gee

    Zachary_gee

    Joined:
    Dec 9, 2016
    Posts:
    5
    SO i was trying to figure out this scripting for multiplayer shooting. i have been working off of the multiplayer shooting tutorial and for some reason when i start up the server and i have the host and a separate client, the host can move around and shoot in any direction but the client can only shoot straight, regardless of my camera positioning. was just trying to figure out how to shoot my bullet the direction its facing.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. using UnityStandardAssets.Characters.FirstPerson;
    6.  
    7. public class PlayerNetwork : NetworkBehaviour
    8. {
    9.     public GameObject FPSCamera;
    10.     public CharacterController characterController;
    11.     public FirstPersonController fpsController;
    12.     public GameObject bulletPrefab;
    13.     public Transform bulletSpawn;
    14.  
    15.     private Transform playerTransform;
    16.  
    17.     // Player Position Vars
    18.     [SyncVar]
    19.     private Vector3 syncPosition;
    20.     public float movementLerpRate = 15;
    21.  
    22.     //Player Rotation vars
    23.     [SyncVar]
    24.     private Quaternion syncPlayerRotation;
    25.     public float rotationLerpRate = 15;
    26.  
    27.     void Start()
    28.     {
    29.         playerTransform = GetComponent<Transform>();
    30.  
    31.         if (!isLocalPlayer)
    32.         {
    33.             FPSCamera.SetActive(true);
    34.             characterController.enabled = false;
    35.             fpsController.enabled = false;
    36.         }
    37.     }
    38.  
    39.     // This [Command] code is called on the Client …
    40.     // … but it is run on the Server!
    41.     [Command]
    42.     void CmdFire()
    43.     {
    44.         // Create the Bullet from the Bullet Prefab
    45.         var bullet = (GameObject)Instantiate(
    46.             bulletPrefab,
    47.             bulletSpawn.position,
    48.             bulletSpawn.rotation);
    49.  
    50.         // Add velocity to the bullet
    51.         bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6;
    52.  
    53.         // Spawn the bullet on the Clients
    54.         NetworkServer.Spawn(bullet);
    55.  
    56.         // Destroy the bullet after 2 seconds
    57.         Destroy(bullet, 2.0f);
    58.     }
    59.  
    60.  
    61.     public void LeaveServer()
    62.     {
    63.         Debug.Log("Leaving server" + Time.timeSinceLevelLoad);
    64.         Network.Disconnect();
    65.     }
    66.  
    67.     public override void OnStartLocalPlayer()
    68.     {
    69.         GetComponentInChildren<Camera>().enabled = true;
    70.     }
    71.  
    72.     public void Update()
    73.     {
    74.         if (!isLocalPlayer)
    75.         {
    76.             return;
    77.         }
    78.  
    79.         if (Input.GetButtonDown("Fire1"))
    80.         {
    81.             CmdFire();
    82.             GetComponentInChildren<Animation>().Play("PistolShoot");
    83.         }
    84.        
    85.     }
    86.     private void FixedUpdate()
    87.     {
    88.         LerpPosition();
    89.         SendPosition();
    90.         LerpRotation();
    91.         SendRotation();
    92.     }
    93.  
    94.     private void LerpPosition()
    95.     {
    96.         if (!isLocalPlayer)
    97.         {
    98.             playerTransform.position = Vector3.Lerp(playerTransform.position, syncPosition, Time.deltaTime * movementLerpRate);
    99.         }
    100.     }
    101.  
    102.     [Command]
    103.     private void CmdProvidePosToServer(Vector3 position)
    104.     {
    105.         syncPosition = position;
    106.     }
    107.  
    108.     [ClientCallback]
    109.     private void SendPosition()
    110.     {
    111.         if (isLocalPlayer == true)
    112.             CmdProvidePosToServer(playerTransform.position);
    113.     }
    114.  
    115.     private void LerpRotation()
    116.     {
    117.         if (!isLocalPlayer == true)
    118.         {
    119.             playerTransform.rotation = Quaternion.Lerp(playerTransform.rotation, syncPlayerRotation, Time.deltaTime * rotationLerpRate);
    120.         }
    121.     }
    122.  
    123.     [Command]
    124.     private void CmdProvideRotationToServer(Quaternion playerRotation)
    125.     {
    126.         syncPlayerRotation = playerRotation;
    127.     }
    128.  
    129.     [ClientCallback]
    130.     private void SendRotation()
    131.     {
    132.         if (isLocalPlayer)
    133.         {
    134.             CmdProvideRotationToServer(playerTransform.rotation);
    135.         }
    136.     }
    137. }
    138.  
    Also the bulletspawn im using is connected to the camera (FPSCamera). thanks in advance for the help!
     
  2. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    I think for your CommandFire I would pass all needed data at the time of the shot like position & rotation.
     
  3. abelitskaja

    abelitskaja

    Joined:
    Jun 18, 2017
    Posts:
    2
    same problem, has anyone solved it?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What is wrong with Zullar's suggestion?