Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by quintonleongnam · May 12, 2020 at 10:30 AM · raycastfpsunityeditorshootingbuilds

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;&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);
     }
 }


alt text

screenshot-19.png (239.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!^^

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image quintonleongnam · Sep 16, 2020 at 08:06 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

212 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FPS shooting 1 Answer

How do i make a Railgun style effect with a raycast shot? 1 Answer

How can I make a Grapple Gun work with the Character Controller? 1 Answer

raycast spawn problem 0 Answers

shooting multiple enemies using raycast 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges