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 /
avatar image
1
Question by EliteHedgehog56 · Apr 24, 2018 at 02:44 AM · raycastvisibledrawray

Make Ray visible

I want to make a railgun/instagib rifle variant of my raycast shoot script, How do I make the bullet ray visible ?

 [RequireComponent(typeof(AudioSource))]
 
 
 public class TestScript : MonoBehaviour {
     public AudioClip shootFX;
     AudioSource audioSource;
     Ray bulletRay;
     RaycastHit rayHit;
     public int dist = 45; //distance ray/bullet travel
     public float lastfired;
     public float FireRate = 10;
     public float dmg = 10;
     public Transform muzzleflash;
 
     void Start ()
     {
         audioSource = GetComponent<AudioSource>();
     }
 
     void Update () {
         if (Input.GetAxis("Fire1")>0f)
         {
             if (Time.time - lastfired > 1 / FireRate)
             {
                 lastfired = Time.time;
                 bulletRay = new Ray(transform.position, transform.forward*dist);
                 Debug.DrawRay (transform.position, transform.forward*dist, Color.yellow);
                 audioSource.PlayOneShot(shootFX, 0.7F);
                 Instantiate (muzzleflash,transform.position,transform.rotation);
 
                 if (Physics.Raycast(transform.position, transform.forward, out rayHit,dist))
                 {
 
                     if(rayHit.transform.gameObject.tag == ("Player")) 
                         rayHit.transform.SendMessage("Damage",dmg);
 
 
                     
                 }
             }
         }
 }
 }

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

Answer by tormentoarmagedoom · Apr 24, 2018 at 07:24 AM

Good day.

I dont know if you can make the ray visible itself. What i normally do i generate a line with the LineRenderer Component with only 2 positions. the intial and the hit position of the raycast.

Is very simple to use, and can create any kind of line.


At some script in the raygun Object, add something like this (I'm wrinting this from memory, maybe some code have an error):

 LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>();
 lineRenderer.material = materialyouwant;
 lineRenderer.widthMultiplier = 0.2f;
 lineRenderer.positionCount = 2;
 Vector3 [] Arraywithpositions = new Vector3[2];
 Arraywithpositions[0] = RailGunposition;
 Arraywithpositions [1] = ImpactPosition;
 lineRenderer.SetPositions(Arraywithpositions);

Anyway, is 100% recommended to check this 2 pages ( 1 & 2 ), test & learn by yourself how to correctly use the line renderer. Is a very powerfull tool!


Rememebr to accept the answer!!

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 EliteHedgehog56 · Apr 24, 2018 at 07:46 AM 0
Share

@tormentoarmagedoom

sounds easy enough :D what would I have to add to the script to make it work, I can’t seem to find an example on the guide

avatar image tormentoarmagedoom EliteHedgehog56 · Apr 25, 2018 at 08:28 AM 0
Share

Extended answer :D

avatar image EliteHedgehog56 tormentoarmagedoom · Apr 25, 2018 at 10:13 PM 0
Share

@tormentoarmagedoom cool thanks dude, I'll see if it works then I'll get back to you

avatar image RLin · Apr 25, 2018 at 11:18 PM 0
Share

See my answer. Debug.DrawRay does indeed work as intended.

avatar image
0

Answer by RLin · Apr 25, 2018 at 11:17 PM

Use debug.drawray, but make sure the third and fourth parameters are filled in as well.

Debug.DrawRay (transform.position, transform.forward*dist, Color.yellow,1,true);

The 1 is the duration that the ray is drawn for. Without filling in that parameter, I assume it defaults to 0, so the ray doesn’t show.

The Unity Docs Page

Another similar question

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 EliteHedgehog56 · Apr 26, 2018 at 12:54 AM 0
Share

@RLin the ray still only shows on the scene view not the game view

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

114 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

Related Questions

DrawRay not drawing a ray 1 Answer

Raycast doesn't work properly? 0 Answers

Is object at least partly visible? 1 Answer

Debug Draw for BoxCast and CapsuleCast 0 Answers

Problem With Physics.Raycast 1 Answer


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