- Home /
FPS shoot mechanic works in editor, but not after build!
Hello Unity Community! How are you? I'm wondering if anyone might be able to help me? So I've been trying to solve a game breaking bug in my FPS game for the last two weeks now and I feel like I've tried absolutely everything!>< I'm desperate for help!
The issue:
The shoot mechanic of my FPS works beautifully in the editor, but after build the mechanic either works after pressing shoot 7 to 20 times or not at all(mostly just doesn't work in the build).
What I have in the scene (of relevance):
An FPS controller with shoot script that uses raycast from viewport center. an enemy with enemy AI script it's original capsule collider and a sphere collider set as a trigger (for use as a way to "damage" the player)
List of things I have subsequently tried to solve the problem:
Used Debug.Log to ensure script is executing desired code. Used Debug.DrawRay to confirm that the Ray is indeed colliding with the enemy Recoded the RayCast in 3 ways ( all which work perfectly in the editor but not in build) Specified a length for the ray Unspecified a length for the ray Added a layerMask and set the enemies to the correct layer (used the bitshift operator). used Visual Studios debug tools to set a breakpoint and step into the code ( no major errors revealed) looked at output.txt / player.log file to see errors, but unsure as how to read its information. Exported project as a package and reimported to a new project. Installed a NEW version of UNITY and built project there. turned off the Raycast target checkbox for all UI elements ( as making UI elements a Raycast target, may "catch" the ray, so thought this might be blocking it) changed collider collision detection type to continuous speculative. took away sphere collider and made the core capsule collider a trigger instead (to use for the damage mechanic). checked box labelled "Update while offscreen" in skin mesh renderer of the enemy. turned off particle effect of weapon (out of fear the effect may be disrupting the ray) checked stop NaN box. changed the order of the colliders in the enemy. prebaked the collision mesh data. changed camera depth to -1 on the main camera (that sits on the player). changed screen size to fit the platform I am building for. (tried standalone 1024 x 768 & 16:9) in player settings set build screen size to full screen and then tried maximized screen.
After trying all the above individually one by one, each time the editor shows the enemy has been detected and the code runs perfectly, but after building none of the 26 builds I have made have yielded a result.
Would anyone be able to help me?
Below is the code for my shoot script. I will also include pictures of my breakpoint debuging in visual studio.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour
{
[SerializeField]
private AudioSource _source;
[SerializeField]
private AudioClip _WetwipeSound;
[SerializeField]
private AudioClip _sanitizerSound;
[SerializeField]
private AudioClip _MopSound;
private Animator _anim;
[SerializeField]
private ParticleSystem _smokeParticle, _muzzleFlash, _tracer, _sprayParticle;
[SerializeField]
private bool _canFire = true;
[SerializeField]
private GameObject _bloodSplat;
[SerializeField]
public bool _isShotgun = false;
[SerializeField]
public bool _isSanitizer = false;
[SerializeField]
public bool _isWetwipes = false;
[SerializeField]
public bool _isMop = false;
public GameObject _spray;
void Start()
{
_anim = GetComponentInChildren<Animator>();
_source = GetComponent<AudioSource>();
}
void Update()
{
//if left click
if (Input.GetMouseButtonDown(0) &amp;&amp; _canFire == true)
{
if (_isShotgun == true)
{
ShotgunShoot();
}
if(_isSanitizer == true)
{
SanitizerShoot();
}
if(_isWetwipes == true)
{
WetwipesShoot();
}
if(_isMop == true)
{
MopShoot();
}
}
}
public void ShotgunShoot()
{
_canFire = false;
_anim.SetTrigger("Fire");
_source.Play();
_smokeParticle.Emit(5);
_tracer.Emit(10);
_muzzleFlash.Emit(1);
StartCoroutine(WeaponCoolDownRoutine());
//cast ray from center of the screen through the radicule
Ray rayOrigin = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f));
RaycastHit hitInfo; //Store information about the object we hit
//cast a ray
if (Physics.Raycast(rayOrigin, out hitInfo, Mathf.Infinity, 1 << 9))
{
EnemyAI enemy = hitInfo.collider.GetComponent<EnemyAI>();
if (enemy != null)
{
GameObject blood = Instantiate(_bloodSplat, hitInfo.point, Quaternion.LookRotation(hitInfo.normal));
Destroy(blood, 5.0f);
enemy.Damage(20);
}
}
}
public void SanitizerShoot()
{
_canFire = false;
_anim.SetTrigger("Fire");
_source.clip = _sanitizerSound;
_source.Play();
_spray.SetActive(true);
StartCoroutine(SprayCoolDownRoutine());
//cast ray from center of the screen through the radicule
Ray rayOrigin = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f));
RaycastHit hitInfo; //Store information about the object we hit
//cast a ray
if (Physics.Raycast(rayOrigin, out hitInfo, Mathf.Infinity, 1 << 9))
{
EnemyAI enemy = hitInfo.collider.GetComponent<EnemyAI>();
if (enemy != null)
{
GameObject blood = Instantiate(_bloodSplat, hitInfo.point, Quaternion.LookRotation(hitInfo.normal));
Destroy(blood, 5.0f);
enemy.Damage(20);
}
}
}
public void WetwipesShoot()
{
_canFire = false;
_anim.SetTrigger("Fire");
_source.clip = _WetwipeSound;
_source.Play();
StartCoroutine(WeaponCoolDownRoutine());
//cast ray from center of the screen through the radicule
Ray rayOrigin = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f));
RaycastHit hitInfo; //Store information about the object we hit
//cast a ray
if (Physics.Raycast(rayOrigin, out hitInfo, Mathf.Infinity, 1 << 9))
{
EnemyAI enemy = hitInfo.collider.GetComponent<EnemyAI>();
if (enemy != null)
{
GameObject blood = Instantiate(_bloodSplat, hitInfo.point, Quaternion.LookRotation(hitInfo.normal));
Destroy(blood, 5.0f);
enemy.Damage(50);
}
}
}
public void MopShoot()
{
_canFire = false;
_anim.SetTrigger("Mop");
_source.clip = _MopSound;
_source.Play();
StartCoroutine(WeaponCoolDownRoutine());
//cast ray from center of the screen through the radicule
Ray rayOrigin = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f));
RaycastHit hitInfo; //Store information about the object we hit
//cast a ray
if (Physics.Raycast(rayOrigin, out hitInfo, Mathf.Infinity, 1 << 9))
{
EnemyAI enemy = hitInfo.collider.GetComponent<EnemyAI>();
if (enemy != null)
{
GameObject blood = Instantiate(_bloodSplat, hitInfo.point, Quaternion.LookRotation(hitInfo.normal));
Destroy(blood, 5.0f);
enemy.Damage(30);
}
}
}
IEnumerator WeaponCoolDownRoutine()
{
yield return new WaitForSeconds(1.5f);
_canFire = true;
}
IEnumerator SprayCoolDownRoutine()
{
yield return new WaitForSeconds(1.5f);
_canFire = true;
_spray.SetActive(false);
}
}
Answer by quintonleongnam · May 18, 2020 at 11:23 AM
After 2 weeks and 35 builds later, I finally solved it! So according to the script API documentation on Character controllers, Unity's character controller component moves the character WITHOUT THE NEED FOR A RIGID BODY and it was that small detail that was where the answer lay! I had previously added a rigid body to my main character / FPS Controller as I thought that it would drop through the floor without one, but the nav mesh and the nature of the character controller itself would have taken care of the falling through the floor and so the ray seems to have been obstructed by the rigid body! removing it made the shoot mechanic work! Really funny that that should be the cause>< I found the solution in the FPS portion of Jon Weinbergers ultimate guide to game development from GameDevHQ!^^
Answer by speedyfast734 · Sep 16, 2020 at 07:59 AM
Omg @quintonleongnam I have been trying to work out why my raycast was not working for my game. I really did not think that it would be that simple, u are a life savour.
@speedyfast734 Hey there! I know right! the simplicity of it all baffles me as well! I'm so glad it helped you with your project! I hope the game making is going well! happy coding!
Your answer
Follow this Question
Related Questions
Raycast shooting in the middle of the screen 1 Answer
Raycasr in my fps? 1 Answer
Multiplayer FPS Bullets: should they be rigid bodies or raycast? 1 Answer
How to get bullets to hit crosshair 2 Answers
fps shooting enemy 1 Answer