Do I understand right, that thisone sould draw a line normally from camera to hitpoint? I attached script to a cube and to cam, but nothing happens, I just see a really lazy cube hanging around when I move my mouse.
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
Debug.DrawLine (ray.origin, hit.point);
}
Looks ok.
Are you looking in the scene view? You will only see the line in the scene view, not the game view.
You’ll see it in the game view too if you turn gizmos on.
–Eric
Just to be sure about complete thing, what I have is:
- a flatten cube placed 0,0,0
- one directional light
- main camera
I’ve classical splitview to work, fullscreen on run game mode, gizmos activated.
do you have collider on your cube and is this placed in the update on on mouse click?
I placed a normal cube, so it have normal collider. The mouse update thing I don’t understand.
Sorry what i meant was, where are you actually placing the code? Are you running it on every frame or did you put in a mouse down event?
Ok, I did solve the problem at last. I don’t know if it is possible to use
Debug.DrawLine (ray.origin, hit.point); but it would be the wrong one for me. I needed
Debug.DrawLine (transform.position, hit.point);
When touching the flatten plate, the ray is starting in the middle of it and as long as the mouse is distanced from middle.
I’ll try to combinate this first goal with the car tutorial for wheel collider stuff, cos there is sort of distance programming. Maybe this is the one I need. Here’s last code I use:
function Update(){
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 10)) {
Debug.DrawLine (transform.position, hit.point);
transform.renderer.material.color = Color.magenta;
}
else transform.renderer.material.color = Color.grey;
}