- Home /
Debug.DrawRay from camera
Hi. I'm fairly new to unity and have been on it for a couple weeks now.
So I'm currently trying to draw a ray from my camera to wherever I click. I've got the ray barely working.
I see the ray only in scene and not game mode. I've tried turning it on in gizmos, setting a time duration for the ray, and already put it in the update function but I still just can't see it in game. Only scene.
Also when I run it, the ray is already there without me having to click on any point. Regardless of where I click, the ray doesn't change to where I click as well. Here's what I have so far:
if(Input.GetButton("LMB")){
var ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay (ray.origin, ray.direction * 50000000, Color.red);
}
Would really appreciate if I could get some advice on this! Thanks!
Debug only shows in the editor, NOT in tgameplay. that's that
generally, you must add a time value
do not make the length "5 million" make it a real world value. (say, 2 meters.) everything in your game $$anonymous$$UST use actual meters and $$anonymous$$G. if you have a person it must be about 2m tall, weigh about 70kg. if you have a car it must be about 3m long, weigh 1000 kg. and so on. whatever actual size your scene is make the ray appropriate
@Fattie: No. There's no problem making the length 5$$anonymous$$. It's just supposed to be "a really long line", and 5$$anonymous$$ is a valid number. Also Unity doesn't use kg at all. $$anonymous$$ass is arbitrary, and frequently should not use real-world values since the physics engine will have floating-point precision issues with values that are too high or low. Generally try to keep mass in the range 0.1 to 100.0.
TBC - I did not say there was any problem with making the length a $$anonymous$$dless figure. I just told him not to do it. I'm bossy with these newbie friends :)
I$$anonymous$$O he should "have a grasp" of his scene and at least know how big it is (in arbitrary units) For me, putting the 5 million in there is a sign of being totally lost.
All three units are arbitrary, but, one must be aware of / change for etc the many ubiquitous systems that use the standard units, eg gravity.
All units are arbitrary, but, you must ("in your head") have real-world ratios between all (particularly masses) or nothing will work in a physical game.
(You've previously mentioned that vid games are tweaked for a good result, this is unrelated to the very common beginner problem where they have mass or scale ratios that are HUGELY out by orders of magnitude !)
A very worthwhile tip for beginners is to use real-world units to avoid the big & common specific problems they often fall in to (ie, the two problems explained in my 3rd and 4th paragraph here).
OR ** if they are superintelligent beginners, they can go ahead and do what experts often do.
Further you are of course utterly correct about the size-of-values issue, but it is a terribly $$anonymous$$or concern for someone who is .... let's say ... 5 million arbitrary units away from just getting a scene working in a basic way! :)
By all means - I should preface my comments by saying "experts can ignore the following because they can ratio anything arbitrarily in their head and never have 'wildly out of scale' problems with the three units..." For bare beginners I'm guilty of omitting that qualifier! :)
You don't understand...mass is just mass, it has no relation to anything except other mass. A sphere with a mass of 1.0 will behave exactly like a sphere with a mass of 100.0, until such time as it collides with another object. The best thing you can do is make your main actor have a normalized mass of 1.0, and everything else be relative to that. Everybody understands that, relative to 1.0, 0.5 is half that and 2.0 is twice that.
As for 5 million, it's just a big number. It doesn't matter what it is; it could be $$anonymous$$athf.Infinity (which might actually be a bit better since that more clearly indicates "this is just a really big number"). Nobody needs to be told "don't do that", including beginners, since it's arbitrary and has no effect on anything.
Answer by MibZ · Oct 01, 2012 at 03:07 PM
Unity's DrawRay function only shows up in the editor view, but if you're willing to buy a package off the asset store, "Vectrosity" by Starscene Software has a DrawRay function that works in the Game view during runtime.
Answer by Eric5h5 · Oct 01, 2012 at 04:11 PM
You need to use Camera.main to get the main camera. Also, the reason the line will never show in the game view even with gizmos on is because it's oriented directly away from the camera. If you try to look at a 1-dimensional line from the end, you won't be able to see it, since it would just be an infinitely small dot.
If you want the line to go from Input.mousePosition to a point in front of the camera's transform, do this:
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawLine (ray.origin, Camera.main.transform.forward * 50000000, Color.red);
Your answer
Follow this Question
Related Questions
Debug Draw for BoxCast and CapsuleCast 0 Answers
Rays drawn using Debug.DrawRay "fall" in editor view 1 Answer
How to calculate Debug.DrawRay length to match raycast 1 Answer
draw ray weird outcome. 2 Answers
Raycast and drawray problems 2 Answers