Rays drawn using Debug.DrawRay "fall" in editor view

I’m casting five rays from my camera to determine where camera is looking. I’m also using Debug.DrawRay to make them visible in editor mode. However, when I run the game and switch to editor something odd starts happening.

There is one set of rays where the rays should be, but they are flashing. Another set of (solid, non-flashing) rays fall down from where the other rays are. I have no idea why this is happening. Here’s my code:

var top : Vector3;
var right : Vector3;
var bottom : Vector3;
var left : Vector3;

var castDirection : Vector3;
castDirection.z = 1;
    
var topx : Vector3;
var rightx : Vector3;
var bottomx : Vector3;
var leftx : Vector3;

var topxs : String = topx.ToString();
var rightxs : String = rightx.ToString();
var bottomxs : String = bottomx.ToString();
var leftxs : String = leftx.ToString();

   
private var hit1 : RaycastHit;
private var hit2 : RaycastHit;
private var hit3 : RaycastHit;  
private var hit4 : RaycastHit;
private var hit5 : RaycastHit;

private var hit1Bool : boolean;
private var hit2Bool : boolean;
private var hit3Bool : boolean;
private var hit4Bool : boolean;
private var hit5Bool : boolean;
    
function Update () {

topx = transform.position + top;
rightx = transform.position + right;  
bottomx = transform.position + bottom;  
leftx = transform.position + left;

	   //Rays from crosshair
	   //Center ray/hit1
	   if(Physics.Raycast(transform.position, castDirection, hit1, 2)){


	   }

	   //Top ray/hit2
	   if(Physics.Raycast(topx, castDirection, hit2, 2)){

	   }

	   //Left ray/hit3
	   if(Physics.Raycast(leftx, castDirection, hit3, 2)){   	   	


	   }

	   //Bottom ray/hit4
	   if(Physics.Raycast(bottomx, castDirection, hit4, 2)){
	   	

	   }

	   //Right ray/hit5
	   if(Physics.Raycast(rightx, castDirection, hit5, 2)){   	   	

	   }   	   

	   //Editor/debug rays
  	   //Center debug ray
       Debug.DrawRay(transform.position, castDirection * 2, Color.green);

	   
	   //Top debug ray
       Debug.DrawRay(topx, castDirection * 2, Color.green);

       
       //Right debug ray
       Debug.DrawRay(rightx, castDirection * 2, Color.green);

  
       //Bottom debug ray
       Debug.DrawRay(bottomx, castDirection * 2, Color.green);

       
       //Left debug ray
       Debug.DrawRay(leftx, castDirection * 2, Color.green);
 }

Edit: The variables top, right, bottom and left are set in the Inspector to (0,1,0), (1,0,0), (0,-1,0) and (-1,0,0) respectively.

I’m using the latest version of Unity, and my scene isn’t processor-intensive in the slightest (primitives with no textures and just one light).

Can anyone identify what’s happening here?

Do you initialize top, bottom, right and left at the Inspector? Because with this code they all will be equal to Vector3.zero, what would let topx, rightx, bottomx and leftx = transform.position.

Apologies, they are set in the Inspector. I've updated the question accordingly.

1 Answer

1

I tested this script, and nothing strange happened. From the camera position, 5 green rays go in the forward direction, as it should be. They don’t flash, nor are other rays appearing in the scene. Nothing in this script can do this, so I suspect something else must be the culprit - probably another instance of this same script forgotten in some object, and with castDirection.y = 1. If you can’t find any lost instance of this script, I suggest you to create a new empty scene and test the script there.

Thanks a lot! I wonder why I didn't try it in a new scene in the first place >.> I'll try that out later today and get back to you :D

Well, I tried it in a new scene and it was fine. So I tried it in my main scene without any other assets except for the light and the floor, and it didn't work. in the end I just delete and re-added the First Person Controller and it was fine. No idea why it was messing up. Thanks a lot for your help! Really highlighted that I should try re-importing/deleting and replacing assets after checking for code errors!