Help on FPS tutorial

Updated on May 22, 2018 in [A] Tutorials
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on May 19, 2018

Greetings Brackeys community.

 

 

 

Currently I’m having trouble with the FPS Tutorial, right now it would seem that my Raycasts are not working, what so ever. Whenever I shoot, things like the muzzle flash are functional, but when shooting another player, no damage is inflicted, nor do the bullet hit effects spawn in at all.

 

 

 

here is my player shoot script

 

Also, seems as if indentation from copy pasting is obsolete, so it may be a big challenging to read my code.

 

 
 
 
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
 
[RequireComponent(typeof(Light))]
[RequireComponent(typeof(WeaponManager))]
[RequireComponent(typeof(WeaponGraphics))]
public class PlayerHSShoot : NetworkBehaviour
{
 
private const string PLAYER_TAG = "Player";
 
private RaycastHit _hit;
 
public GameObject Character;
 
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();
 
}
 
void Update()
{
currentWeapon = weaponManager.GetCurrentWeapon();
 
if (currentWeapon.fireRate <= 0f)
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
else
{
if (Input.GetButtonDown("Fire1"))
{
 
InvokeRepeating("Shoot", 0f, 4f / currentWeapon.fireRate);
 
}
else if (Input.GetButtonUp("Fire1"))
{
 
CancelInvoke("Shoot");
 
}
}
}
 
[Command]
void CmdOnHit (Vector3 _pos, Vector3 _normal)
{
 
RpcDoHitEffect(_pos, _normal);
 
}
 
[ClientRpc]
void RpcDoHitEffect(Vector3 _pos, Vector3 _normal)
{
 
GameObject _hitEffect = (GameObject)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()
{
if (!isLocalPlayer)
{
 
return;
 
}
 
CmdOnShoot();
 
Vector3 myTransform = Character.transform.forward;
 
if (Physics.Raycast(weaponCam.transform.position, weaponCam.transform.forward, out _hit, 20f, mask))
{
 
if (_hit.collider.gameObject.CompareTag("Environment"))
{
CmdOnHit(_hit.point, _hit.normal);
}
else if (_hit.collider.tag == PLAYER_TAG)
{
CmdPlayerShot(_hit.collider.name, currentWeapon.weaponDamage);
 
}
 
CmdOnHit(_hit.point, _hit.normal);
 
}
}
 
[Command]
void CmdPlayerShot(string _playerID, int _damage)
{
 
Debug.Log(_playerID + " has been shot.");
 
Player _player = GameManager.GetPlayer(_playerID);
_player.RpcTakeDamage(_damage);
 
}
 
}
 
  • Liked by
Reply
1 on May 22, 2018

bump

Wise
on May 22, 2018

Are you hitting anything with the raycast? Is anything on the “mask” Layer Mask at all?

Show more replies
  • Liked by
Reply
Cancel