So, my raycasting doesn’t seem to work, at all. (Yes, I’m using Brackey’s FPS tutorial)
Couple more problems;
- where I have the “Shoot command initiated” console log, it shows up exactly 2 times as much as all the other console logs. I attempted inserting these console logs to make sure that the methods were working correctly.
- A random 2nd object seems to appear, out of nowhere in the scene, checking the objects in the scene, it is not stated that it is even IN the scene. So I’m overall just confused. I’ll include a Screenshot.
(The object in the bottom left of the Game view isn’t in the hierarchy, the scene view, or any other view. This seems to happen only when there is a second player in the game. Yes, I placed the player there intentionally) - The null reference in the screenshot is referring to my “fireRate” method, and it seems to appear and disappear out of nowhere, as it functions correctly every time. I just removed it entirely because of the annoyance. But a solution would be nice if you had any ideas.
- NONE of my Hit effects work.
EDIT; After a bit of testing, I have managed to get the Drawline of my raycast to function, but otherwise the raycast is still not functional.
Here is my PlayerShoot.cs;
using UnityEngine;
using UnityEngine.Networking;
[RequireComponent(typeof(WeaponManager))]
[RequireComponent(typeof(WeaponGraphics))]
public class PlayerShoot : NetworkBehaviour
{
private const string PLAYER_TAG = "Player";
private PlayerWeapon currentWeapon;
[SerializeField]
private Camera weaponCam;
[SerializeField]
private LayerMask mask;
private PlayerWeapon GetCurrentWeapon;
private WeaponManager weaponManager;
void Start()
{
if (weaponCam == null)
{
Debug.LogError("PlayerShoot: No camera referenced");
this.enabled = false;
}
weaponManager = GetComponent<WeaponManager>();
}
void Update()
{
currentWeapon = weaponManager.GetCurrentWeapon();
if (Input.GetButtonDown("Fire1"))
{
InvokeRepeating("Shoot", 0f, 0.3f / currentWeapon.fireRate);
InvokeRepeating("Raycast", 0f, 0.3f / currentWeapon.fireRate);
}
else if (Input.GetButtonUp("Fire1"))
{
CancelInvoke("Shoot");
CancelInvoke("Raycast");
}
}
[Command]
void CmdOnHit (Vector3 _pos, Vector3 _normal)
{
RpcDoHitEffect(_pos, _normal);
}
[ClientRpc]
void RpcDoHitEffect(Vector3 _pos, Vector3 _normal)
{
GameObject _hitEffect = Instantiate(weaponManager.GetCurrentGraphics().hitEffectPrefab, _pos, Quaternion.LookRotation(_normal));
Destroy(_hitEffect, 0.75f);
}
[Command]
void CmdOnShoot()
{
RpcDoFlash();
}
[ClientRpc]
void RpcDoFlash()
{
weaponManager.GetCurrentGraphics().muzzleFlash.Play();
}
[Client]
void Shoot()
{
Debug.Log("Shoot command initiated");
if (!isLocalPlayer)
{
Debug.Log("!isLocalPlayer");
return;
}
CmdOnShoot();
}
[Command]
void CmdPlayerShot(string _playerID, int _damage)
{
Debug.Log(_playerID + " has been shot.");
Player _player = GameManager.GetPlayer(_playerID);
_player.RpcTakeDamage(_damage);
}
void Raycast ()
{
if (!isLocalPlayer)
{
Debug.Log("!isLocalPlayer");
return;
}
RaycastHit _hit = new RaycastHit();
Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
Debug.DrawRay(transform.position, forward, Color.green);
if (Physics.Raycast(weaponCam.transform.position, weaponCam.transform.forward, out _hit, 5000f, mask))
{
if (_hit.collider.tag == "PLAYER_TAG")
{
CmdPlayerShot(_hit.collider.name, currentWeapon.weaponDamage);
}
CmdOnHit(_hit.point, _hit.normal);
}
}
}