Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to cast a Raycast and hit another player that causes text to say that the player has been hit. It wont register hitting another player, but it registers when it hits itself.
Here is the code for the character shooting script, which has the issue above. Also this is all in unity.
C#
using UnityEngine;
using UnityEngine.Networking;

public class PlayerShoot : NetworkBehaviour
{

    private const string PLAYER_TAG = "Player";
   
   public PlayerWeapon weapon;
   
    [SerializeField]
    private Camera cam;

    [SerializeField]
    private LayerMask mask;

    void Start ()
    {
        if(cam == null)
        {
            Debug.LogError ("PlayerShoot; No Camera referenced");
            this.enabled = false;
        }
    }

    void Update ()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Shoot();
        }
    }

    [Client]
    void Shoot ()
    {
        RaycastHit _hit;
        if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask) )
        {
            if (_hit.collider.tag == "Player")
            {
                CmdPlayerShot(_hit.collider.name);
            }
        }
    }

    [Command]
    void CmdPlayerShot (string _ID)
    {
        Debug.Log(_ID + "Has been shot.");
    }
}


What I have tried:

I have tried everything at this point.
Posted
Updated 17-Mar-22 7:23am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900