- Home /
how to make it so i can see a raycast
ok heres my raycast script
var damage : float = 1;
function Update(){
if(Input.GetButtonDown("Fire1")){
FireOneShot();
}
}
function FireOneShot(){
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
if (Physics.Raycast (transform.position, direction, hit, 300)) {
Debug.DrawLine (transform.position, hit.point, Color.cyan);
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
how do i make it so i can see the raycast bullet? i figured that
Debug.DrawLine (transform.position, hit.point, Color.cyan);
would draw a blue line, but it didnt.
Answer by CHPedersen · Sep 08, 2011 at 09:33 AM
Debug.DrawLine works just fine, but you have to give it the proper arguments. If you call it outside of Update, and all you supply is a startpoint, an endpoint and a color, the function draws a line for just 1 frame, like the docs say:
http://unity3d.com/support/documentation/ScriptReference/Debug.DrawLine.html
If your game renders more frames/sec than 60, then chances are the frame that contained the ray got overwritten in the framebuffer before your monitor asked for an update, and consequentially, you'll never see it even for a splitsecond.
Add some number of seconds after the color so the ray stays visible for a time to see it, and remember that the lines drawn by Debug.DrawLine are Gizmos! (IMPORTANT, this had me scratching my head for an unreasonably long time before). This means you have to turn on Gizmos (top right corner, next to the Stats button) to see it, or the ray stays invisible.
The problem with DrawLine and DrawRay is that neither can handle a layermask param. How can I draw a line so that it ignores or only hits certain objects?
Answer by icecold043 · May 02, 2020 at 07:59 AM
I don't understand why they just don't let us make raycasts appear like gizmos in the scene view, if you make a mistake in your drawray, you'll think your raycast is not good so you'll change it, then you'll think it's good but it's not because you changed the value. One mistake and it's broken :/
Your answer
![](https://koobas.hobune.stream/wayback/20220612204726im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
script doesn't do anything 1 Answer
Best way to shoot physical bullets? 2 Answers
Raycasr in my fps? 1 Answer
Need Help with Bullets 1 Answer
Raycast visible bullets 2 Answers