When I shoot my gun from a far distance the raycast doesn’t detect the other player. But when the player is touching the other player it works. here is my script.
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class ShootingScript1 : NetworkBehaviour
{
//public ParticleSystem _muzzleFlash;
//public AudioSource _gunAudio;
public GameObject _impactPrefab;
public Transform ray;
ParticleSystem _impactEffect;
public int gunDamage = 25;
public float range;
public float fireRate;
public LayerMask whatToHit;
void Start()
{
_impactEffect = Instantiate(_impactPrefab).GetComponent<ParticleSystem>();
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
//_muzzleFlash.Stop();
//_muzzleFlash.Play();
//_gunAudio.Stop();
//_gunAudio.Play();
RaycastHit2D hit = Physics2D.Raycast(ray.position, Vector2.zero, range, whatToHit);
if (hit.transform.gameObject.tag == "Player")
{
//Health health = hit.transform.GetComponent<Health>();
_impactEffect.transform.position = hit.point;
_impactEffect.Stop();
_impactEffect.Play();
Debug.Log("Hit");
//Destroy(gameObject);
CmdHitPlayer(hit.transform.gameObject);
}
}
}
[Command]
void CmdHitPlayer(GameObject hit)
{
hit.GetComponent<NetworkedPlayerScript>().RpcResolveHit();
}
}