- Home /
[Unsolved] Raycast isn't casting
Hello,
I make a voxel game, which there is a 100x3x100 area of cubes. I want the cubes that have another cube on top of them to be invisible, for performance issues. I cast a ray from each cube to check if there is another cube on top, but it isn't casyed at all. What's the problem?
#pragma strict
private var pl : Transform;
private var meshComp : MeshRenderer;
function Awake () {
var plObj = GameObject.Find("Player");
pl = plObj.transform;
meshComp = gameObject.GetComponent(MeshRenderer);
}
function Update () {
if(Vector3.Distance(gameObject.transform.position, pl.position) > 20)
{
meshComp.enabled = false;
}
else
{
var fwd = transform.TransformDirection(Vector3.up);
var hit : RaycastHit;
if (Physics.Raycast(transform.position, fwd, hit, 1))
{
print ("There is something in front of the object!");
meshComp.enabled = false;
Debug.DrawLine (transform.position, hit.point, Color.cyan);
}
}
}
Shouldn't this line
var fwd = transform.TransformDirection(Vector3.up);
Be this ins$$anonymous$$d?
var fwd = transform.TransformDirection(Vector3.forward;
No, I want to check on top of the object. The variable name is wrong!
You have colliders on all the object?
You've placed a Debug.Log() in the 'else' to verify that it is firing?
You've verified that this script is on all the blocks?
Note that you are doing 30,000 raycasts per frame if this script is on all your blocks.
Actually, I raycast from blocks that are close to the player. And yes, every cube has collider and the script. Also the Debug.DrawLine doesn't work as well. It doesn't display any line.
So you begin to verify. Put a Debug.Log() in both halves of your if() else() block to see which one is firing. Put a Debug.DrawRay(transform.position, fwd * 1.0) to verify you are casting in the direction you expect and that your Raycast() is long enough. View your blocks in Scene view to verify the collider is aligned and sized to the blocks.
After some research, I found out that if the player is on top, the block is destroyed. The problem is that it doesn't work with cubes on top... -.- Debug.Logs work, but again, if the object on top is the player...
Your answer
Follow this Question
Related Questions
I don't know why i can't detect by ray sth. tagged, 1 Answer
Raycast isn't working on small distances 1 Answer
how to get info on what raycast hitted 1 Answer
Raycast on mini-map 0 Answers
Exclude tag from raycast 1 Answer