- Home /
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);
}
}
}
}
}
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!!
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
@tormentoarmagedoom cool thanks dude, I'll see if it works then I'll get back to you
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.
@RLin the ray still only shows on the scene view not the game view
Your answer
![](https://koobas.hobune.stream/wayback/20220612161411im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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