• 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 MrFlibble · Apr 19, 2014 at 03:02 PM · raycastgunlinerendererlinelaser

Creating a Ray Gun

So I want to create a ray gun for my game using a Line Renderer, and although i thought I got the basic code right I see no laser.

Any help would be appreciated as I'm getting no error messages.

 using UnityEngine;
 using System.Collections;
 
 public class Laser2 : MonoBehaviour {
 
     public float shootTimer;
     public float coolDown;
 
     public float realoadtime = 10f;
     public int clip = 10;
     public int BulletsperClip = 1;
     public int Bulletsremaining = 10;
 
 
     public Color firstColour = Color.magenta;
     public Color secondColour = Color.red;
     public LineRenderer laser;
     public bool isOn;
     public float time;
     public float adj;
 
 
 
 
     // Use this for initialization
     void Start () {
 
         shootTimer = 0.15f;
         coolDown = 0.15f;
 
 
         LineRenderer  laser = (LineRenderer) gameObject.AddComponent("laser");
         laser.material = new Material (Shader.Find("Particles/Additive"));
         laser.SetWidth(0.05f, 0.05f);
         laser.SetVertexCount(2);
         laser.SetColors(firstColour, secondColour);
 
         isOn = false;
     
     }
     
     // Update is called once per frame
     
     void Update () {
         
         time += Time.deltaTime;
         
         if(Input.GetMouseButtonDown(0) && time > 0.5){ 
             
             time = 0.0f;
             
             
             var start = transform.position;
             laser.SetPosition(0,start);
         
             
 
             Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
             RaycastHit hit;
 
             if (Input.GetMouseButton (0) && shootTimer == 0 && Bulletsremaining > 0) {
 
             if(Physics.Raycast(ray, out hit, 25.0f))
             {
                 
                 laser.SetPosition(1, hit.point + hit.normal);
             }
 
 
             
             
             isOn = true;
             OnOff();
 
             }
             
         }
         
         
     }
 
 
 IEnumerator OnOff () {
     
     
     if (isOn == true); {
         yield return new WaitForSeconds (0.05f);
         isOn = false;
         laser.enabled = false;
     }
     
 }
 
     void OnGUI()
     {
         
         GUI.Box (new Rect (10, 50, 150, 20),"Ammo:" + Bulletsremaining + "/" + clip);
         
     }
 
 }
Comment
Add comment · Show 1
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 grahnzz · Apr 19, 2014 at 03:29 PM 0
Share

Do some debugging with your IDE and/or UnityEngine.Debug.Log()

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by grahnzz · Apr 19, 2014 at 03:32 PM

One thing that i see directly by looking at your code is that you are setting up a new instance of a line renderer in Start(), you probably intended to reference the variable laser that is global in the class.

 LineRenderer  laser = (LineRenderer) gameObject.AddComponent("laser");
 //try:
 laser = (LineRenderer) gameObject.AddComponent("laser");
Comment
Add comment · Show 3 · 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 MrFlibble · Apr 19, 2014 at 04:35 PM 0
Share

Thanks for the response but now I'm getting two new errors.

1: NullReferenceException: Object reference not set to an instance of an object Laser2.Start () (at Assets/Scripts/Laser2.cs:33)

and the second is the same but at line 54.

avatar image grahnzz · Apr 19, 2014 at 08:56 PM 0
Share

In your Start() where you initialize your LineRenderer: gameObject.AddComponent("laser") should probably be gameObject.AddComponent("LineRenderer")

avatar image robertbu · Apr 19, 2014 at 09:06 PM 0
Share

Taking a step further, you don't use a string version of AddComponent() if you can avoid it. Use this ins$$anonymous$$d:

    laser = gameObject.AddComponent<LineRenderer>();

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

LineRenderer (Laser Beam) is not following the ray it's going on the wrong direction when reflecting 1 Answer

Dynamically adjusting LineRenderer vertexes to follow raycasts 0 Answers

How to draw a line between objects and get distance 1 Answer

Printing Distance b/w two points using line renderer and Raycast 1 Answer

RayCast Line Not Rotating with Gun? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges