Search Unity

Is there any way to make the line renderer visible to a different player?

Discussion in 'Scripting' started by The3rdUprising, Oct 19, 2017.

  1. The3rdUprising

    The3rdUprising

    Joined:
    Aug 11, 2016
    Posts:
    4
    Hi, this is my first forum post so please bear with me.
    I am currently working on a 2 player FPS game, and I am using a line renderer as a type of laser, that fires from the barrel of your gun to the point where you are aiming using raycasts, and once it reaches its target it instantiates a sphere there. Both players can see their own lines when they shoot, however when I shoot on the client the line is not visible on the host, and visa versa, but the sphere that is instantiated can be seen by both players. I was wondering if there is any way to make the line renderer appear to both players. Thanks!

    my Player script which is attached to the Player prefab and handles the line renderer:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class PlayerShooting : NetworkBehaviour {
    7.  
    8.     float range = 100.0f;
    9.     float elapsedTime;
    10.     bool canShoot;
    11.     [SerializeField] float shotCooldown = .3f;
    12.     [SerializeField] private Transform firePosition;
    13.     private AudioSource audioSource;
    14.     [SerializeField] private AudioClip gunAudio;
    15.     [SerializeField] private GameObject impactPrefab;
    16.  
    17.     void Start()
    18.     {
    19.         if (isLocalPlayer) {
    20.             canShoot = true;
    21.         }
    22.         audioSource = GetComponent<AudioSource> ();
    23.  
    24.         LineRenderer lineRen = gameObject.GetComponent<LineRenderer> ();
    25.         lineRen.material = new Material (Shader.Find("Particles/Alpha Blended"));
    26.         lineRen.startColor = Color.red;
    27.         lineRen.endColor = Color.red;
    28.         lineRen.startWidth = .01f;
    29.     }
    30.        
    31.  
    32.     void Update()
    33.     {
    34.         LineRenderer lineRenderer = gameObject.GetComponent<LineRenderer> ();
    35.         Ray ray = new Ray (firePosition.position, firePosition.forward);
    36.         RaycastHit hit;
    37.         bool result = Physics.Raycast (ray, out hit, 50f);
    38.         Vector3 hitPoint = hit.point;
    39.  
    40.         lineRenderer.enabled = false;
    41.  
    42.         if (!canShoot)
    43.             return;
    44.         elapsedTime += Time.deltaTime;
    45.  
    46.         if (Input.GetButtonDown ("Fire1") && elapsedTime > shotCooldown) {
    47.             elapsedTime = 0f;
    48.             if (result != false) {
    49.                 lineRenderer.enabled = true;
    50.                 lineRenderer.SetPosition (0, firePosition.position);
    51.                 lineRenderer.SetPosition (1, hitPoint);
    52.             }
    53.             CmdFireShot (firePosition.position, firePosition.forward);
    54.         }
    55.     }
    56.  
    57.     [Command]
    58.     void CmdFireShot(Vector3 origin, Vector3 direction){
    59.  
    60.         audioSource.clip = gunAudio;
    61.         audioSource.Play ();
    62.  
    63.         RaycastHit hit;
    64.  
    65.         Ray ray = new Ray (origin, direction);
    66.         Debug.DrawRay (ray.origin, ray.direction * 3f, Color.red, 1f);
    67.  
    68.         bool result = Physics.Raycast (ray, out hit, 50f);
    69.         Vector3 hitPoint = hit.point;
    70.         RpcSpawnImpact (hitPoint);
    71.  
    72.         if (result) {
    73.             PlayerHealth enemy = hit.transform.GetComponent<PlayerHealth> ();
    74.  
    75.             if (enemy != null) {
    76.                 enemy.TakeDamage (50f);
    77.             }
    78.         }
    79.            
    80.     }
    81.  
    82.     [ClientRpc]
    83.     void RpcSpawnImpact(Vector3 hitPoint){
    84.         Instantiate (impactPrefab, hitPoint,Quaternion.identity);
    85.     }
    86.  
    87. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,743
    I'm not familiar enough with the Unity networking to know the precise answer, but basically you want to send over to the other client the begin and end of the ray and then reconstruct it at that end too. You could probably bundle it all together with however you are sending the impact point.
     
  3. The3rdUprising

    The3rdUprising

    Joined:
    Aug 11, 2016
    Posts:
    4
    Can anyone please help?