- Home /
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);
}
}
}
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:
Does it help if you compare these with the code in your project?
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.
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.
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.
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:
- In the Project Window open up the Materials Folder
- Select LineRenderMaterial.
- 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.
- Now click Particles
- 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..
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)
Your answer
Follow this Question
Related Questions
Particle effects along line renderer? 0 Answers
Line Renderer Emission module error 0 Answers
Moving platform not taking mesh with it 0 Answers
3D Platformer Tutorial - 5 Answers
3D Platform Tutorial Problem 1 Answer