Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ayyylex · Sep 09, 2016 at 03:58 PM · c#raycastlinerenderer

LineRenderer Raycast Gun Bug

I have an issue with the LineRenderer component and when I try to implement it into script so that it acts as a visible ray.

Heres the code of the gun that fires the ray:

using UnityEngine; using System.Collections;

[RequireComponent (typeof (AudioSource))] public class dev_ray_shoot : MonoBehaviour {

 public enum GunType {Semi,Burst,Auto};
 public GunType gunType;
 public float rpm;
 
 //Components
 public Transform rayExit;
 private LineRenderer tracer;
 
 //System
 private float secondsBetweenShots;
 private float nextPossibleShootTime;
 
 void Start() {
     secondsBetweenShots = 60/rpm;
     if (GetComponent<LineRenderer>()){
         tracer = GetComponent<LineRenderer>();
     }
 }
 
 public void Shoot() {
     if (CanShoot()){
         Ray ray = new Ray(rayExit.position,rayExit.forward);
         RaycastHit hit;
         
         float rayDistance = 20;
         
         if (Physics.Raycast(ray,out hit, rayDistance = 20)){
             rayDistance = hit.distance;
         }
         
         nextPossibleShootTime = Time.time + secondsBetweenShots;
         
         AudioSource audio = GetComponent<AudioSource> ();
         audio.Play(); 
         if (tracer){
             StartCoroutine("RenderTracer", ray.direction * rayDistance);
         }
         Debug.DrawRay (ray.origin,ray.direction * rayDistance, Color.red, 1);
     }
 }
 
 public void Shoot_Automatic () {
     if (gunType == GunType.Auto) {
         Shoot ();
     }
 }
 
 private bool CanShoot() {
     bool canShoot = true;
     
     if (Time.time < nextPossibleShootTime) {
         canShoot = false;
     }
     
     return canShoot;
 }

 IEnumerator RenderTracer(Vector3 hitPoint){
     tracer.enabled = true;
     tracer.SetPosition(0,rayExit.localPosition);
     tracer.SetPosition(1,rayExit.localPosition + hitPoint);
     yield return null;
     tracer.enabled = false;
 }

}

The linerenderer component which is attatched to the same gameobject as this script does not fully follow the ray the way its supposed to. So can someone please help? I have a picture of it:

http://imgur.com/a/0qibZ

(The yellow line is the linerenderer and the red line is the debug.drawray) for extra information, the LineRenderer deforms when I fire in different directions, it changes its length each time.

Please help, thank you.

P.S. Im using Unity 5

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by doublemax · Sep 09, 2016 at 04:10 PM

Instead of:

 tracer.SetPosition(1,rayExit.localPosition + hitPoint);

Try:

 tracer.SetPosition(1,rayExit.position + ray.direction * hit.distance);

Comment
Add comment · Show 4 · 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 ayyylex · Sep 09, 2016 at 04:31 PM 0
Share

If I just change that it will say the variables are missing/unkown in the current context, due to the those things being declared only in the Update, not in the IEnumerator RenderTracer.

If I add them at the top (after the "$$anonymous$$onobehaviour {" ) where most variables are then it accepts the script but the linerenderer just disappears somewhere unknown.

So it doesn't fully work.

avatar image doublemax ayyylex · Sep 09, 2016 at 05:08 PM 0
Share

For point 0 replace localPosition with position, too.

 tracer.SetPosition(0,rayExit.position);
avatar image ayyylex doublemax · Sep 09, 2016 at 05:20 PM 0
Share

Nope, I set both to have position ins$$anonymous$$d of localPosition and replaced the HitPoint stuff, also added Ray ray, RaycastHit hit at the top of the code with the other variables, but like last time it just disappears.

  • for extra information, the LineRenderer deforms when I fire in different directions, it changes its length each time.

Show more comments

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

237 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 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

Having an issue Making a second ray cast with a new angle 1 Answer

Strange Raycast problem 0 Answers

Scripting Errors 1 Answer

(Raycasts) C# Why am I shooting myself? 0 Answers

Is there any way how to do raycasts like this? 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