• 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
Question by SetiFX · Jan 12, 2015 at 11:00 AM · particle systemtutorialline renderer

Ray (Gunshot) not visible, shadow is, why?

The issue occurs within chapter 7: Harming Enemies of the Survival Shooter Tutorial.

The audio and muzzle flash trigger correctly.

The line renderer of the particle system (Ray shootRay) is not visible when triggered, yet the shadow is visible.

Can someone please advise how I can fix this so I can see the Ray shoot from the gun?

Thanks

 using UnityEngine;
 
 public class PlayerShooting : MonoBehaviour
 {
         public int damagePerShot = 20;                  // The damage inflicted by each bullet.
         public float timeBetweenBullets = 0.15f;        // The time between each shot.
         public float range = 100f;                      // The distance the gun can fire.
     
         float timer;                                    // A timer to determine when to fire.
         Ray shootRay;                                   // A ray from the gun end forwards.
         RaycastHit shootHit;                            // A raycast hit to get information about what was hit.
         int shootableMask;                              // A layer mask so the raycast only hits things on the shootable layer.
         ParticleSystem gunParticles;                    // Reference to the particle system.
         LineRenderer gunLine;                           // Reference to the line renderer.
         AudioSource gunAudio;                           // Reference to the audio source.
         Light gunLight;                                 // Reference to the light component.
         float effectsDisplayTime = 0.2f;                // The proportion of the timeBetweenBullets that the effects will display for.
     
         void Awake ()
         {
                 // Create a layer mask for the Shootable layer.
                 shootableMask = LayerMask.GetMask ("Shootable");
         
                 // Set up the references.
                 gunParticles = GetComponent<ParticleSystem> ();
                 gunLine = GetComponent <LineRenderer> ();
                 gunAudio = GetComponent<AudioSource> ();
                 gunLight = GetComponent<Light> ();
         }
     
         void Update ()
         {
                 // Add the time since Update was last called to the timer.
                 timer += Time.deltaTime;
         
                 // If the Fire1 button is being press and it's time to fire...
                 if (Input.GetButton ("Fire1") && timer >= timeBetweenBullets) {
                         // ... shoot the gun.
                         Shoot ();
                 }
         
                 // If the timer has exceeded the proportion of timeBetweenBullets that the effects should be displayed for...
                 if (timer >= timeBetweenBullets * effectsDisplayTime) {
                         // ... disable the effects.
                         DisableEffects ();
                 }
         }
     
         public void DisableEffects ()
         {
                 // Disable the line renderer and the light.
                 gunLine.enabled = false;
                 gunLight.enabled = false;
         }
     
         void Shoot ()
         {
                 // Reset the timer.
                 timer = 0f;
         
                 // Play the gun shot audioclip.
                 gunAudio.Play ();
         
                 // Enable the light.
                 gunLight.enabled = true;
         
                 // Stop the particles from playing if they were, then start the particles.
                 gunParticles.Stop ();
                 gunParticles.Play ();
         
                 // Enable the line renderer and set it's first position to be the end of the gun.
                 gunLine.enabled = true;
                 gunLine.SetPosition (0, transform.position);
         
                 // Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
                 shootRay.origin = transform.position;
                 shootRay.direction = transform.forward;
         
                 // Perform the raycast against gameobjects on the shootable layer and if it hits something...
                 if (Physics.Raycast (shootRay, out shootHit, range, shootableMask)) {
                         // Try and find an EnemyHealth script on the gameobject hit.
                         EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
             
                         // If the EnemyHealth component exist...
                         if (enemyHealth != null) {
                                 // ... the enemy should take damage.
                                 enemyHealth.TakeDamage (damagePerShot, shootHit.point);
                         }
             
                         // Set the second position of the line renderer to the point the raycast hit.
                         gunLine.SetPosition (1, shootHit.point);
                 }
         // If the raycast didn't hit anything on the shootable layer...
         else {
                         // ... set the second position of the line renderer to the fullest extent of the gun's range.
                         gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
                 }
         }
 }
Comment

People who like this

0 Show 0
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

4 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Adam-Buckner · Jan 13, 2015 at 09:48 PM

This may sound rude, but, have you double checked your steps with the original video series? It works in the original video, so it should work at home. Now, I've quickly scanned the code above and nothing jumped out and shouted "ME! I'm the problem!"

There are "complete assets" included in the project files: From the Asset Store

Does it help if you compare these with the code in your project?

Comment

People who like this

0 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 SetiFX · Jan 14, 2015 at 03:05 AM 0
Share

No, it's not rude. Yes, I did check and recheck numerous times. I found the problem though. For some reason the z value for scale was inadvertently set to a negative value. I am not sure why that happened as there was no cause to change scale. Thanks for responding. It prompted me to check outside the script and in areas not associate with the lesson.

avatar image

Answer by SetiFX · Jan 14, 2015 at 01:54 AM

No, it's not rude. Thanks for responding. And, yes, I have double, triple and quadruple checked prior to contacting you. I have found the issue though and I am not sure how it occurred, but the z value for scale somehow was changed to -4.7 and I have no idea how it happened. It was not something I was looking at for troubleshooting since it was not something part of the tutorial nor something I changed consciously, but eventually it occurred to me that the issue was not in the script and not something obvious. Eventually I found it. I appreciate you taking the time to answer though.

Comment

People who like this

0 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 schmosef · Apr 04, 2015 at 06:46 AM 0
Share

I'm running through the tutorial and have run into the same issue as you.

To rule out anything I might have done to the prefabs, I imported the assets into a new project and tested the included completed scene; it's got the same problem.

Do you remember which game object had the inverted z-axis scale? I can't find it.

Edit: I tried testing the assets in Unity 4.6 (I was using Unity 5 before) and the line rendered correctly. I realised that the problem must be with the shader on the material in Unity 5. I changed it to a different shader and now it renders correctly.

avatar image

Answer by nrussell6 · May 22, 2015 at 09:11 AM

Solution to Problem for Unity 5 (listed as its own comment so that it's easier to locate):

This is a reply to the above post comment by SetiFX. For your reference, the specific reply is as follows:

Edit: I tried testing the assets in Unity 4.6 (I was using Unity 5 before) and the line rendered correctly. I realised that the problem must be with the shader on the material in Unity 5. I changed it to a different shader and now it renders correctly.

For the Experience: The shader you'll want to use is either [Particles - Additive] or [Mobile - Particles - Additive].

Step by Step Guide for the Less Experienced:

  1. In the Project Window open up the Materials Folder
  2. Select LineRenderMaterial.
  3. Underneath the name of the first component in the material, or the LineRenderMaterialComponent, is a drop down menu with the word "Shader" beside it. Click on this menu.
  4. Now click Particles
  5. now click Additive

The effect should work just fine now. Incidentally, at step 4, you could instead click Mobile, then Particles, and then Additive for the same visual effect. From what I understand, and please correct me if I'm wrong, the mobile variant takes less resources than the other. This comes at the cost of reduced image quality, true, but for a short effect like this the difference isn't really noticeable.

Additionally, for those who are curious, the LineRenderMaterial posted above is the exact same component that you put into the GunBarrelEnd-Line Renderer Component - Materials - Element 0] slot that you set earlier on in the tutorial..

Comment

People who like this

0 Show 0 · 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

Answer by whencut999 · Jun 09, 2017 at 09:01 PM

i have done all the changes mentioned above but i cant kill enemies the line render is not showing

plz help me

the error i get is

LineRenderer.SetPosition index out of bounds! UnityEngine.LineRenderer:SetPosition(Int32, Vector3) PlayerShooting:Shoot() (at Assets/Scripts/Player/PlayerShooting.cs:85) PlayerShooting:Update() (at Assets/Scripts/Player/PlayerShooting.cs:39)

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Line Renderer Emission module error 0 Answers

Particle effects along line renderer? 0 Answers

Where can I find a tutorial on the fps kit by onemanarmy? 1 Answer

Unity 4.x Stencil Buffer tutorial...? 1 Answer

How does Unity figure out generic component type as Wall in Project: 2D Roguelike tutorial? 0 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