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.
– aldonalettoApologies, they are set in the Inspector. I've updated the question accordingly.
– anon76980169