Question by
MichaelKingsman · Aug 10, 2019 at 07:07 AM ·
physicsraycastmathflayermask
Raycast max Distance is always low and infinity not working [ Solved ]
i solved myself answer is: that debug.drawray is useless and dont present the real ray at all . we should use debug.drawline instead .
hi , im making strategy game wich there is an option to Build Buildings . when u click building icon to create it . frist a ghost mode of that building will appear . this ghost mode . have 4 empty object in all corners of it . and they cast a ray towards down to hit ground and if greatest difference between this 4 ray ( length) is greater than 1f . then ghost mode turns Red , means u cant create there . ground need to be flat to create . Issue is these 4 rays are very short ,even with mathf.infinity as max distance here is the code :
LayerMask GroundOnly = MenuSetup.Instance.GroundOnly;
if (transform.Find("HeightPoints"))
{
// get heights
GameObject HeightPointObj = transform.Find("HeightPoints").gameObject as GameObject;
int heightPoints = HeightPointObj.transform.childCount;
float[] heights = new float[heightPoints];
for(int i = 0; i<heightPoints; i++)
{
GameObject point = HeightPointObj.transform.GetChild(i).gameObject as GameObject;
RaycastHit hit;
// ------------------------------------------------Issue is here ----------------------------------------------------
if (Physics.Raycast(point.transform.position, Vector3.down,out hit, 1000f,GroundOnly))
{
heights[i] = hit.point.y;
Debug.DrawRay(point.transform.position,Vector3.down,Color.yellow,1);
}
}
Debug.Log(heights[0]+" "+ heights[1]+ " " + heights[2]+ " " + heights[3]);
// test wich height is tallest and shortest
tallestheight = 0f;
shortestheight = 100f;
for (int j=0; j < heightPoints; j++)
{
if (heights[j] > tallestheight)
tallestheight = heights[j];
if (heights[j] < shortestheight)
shortestheight = heights[j];
}
// the diffrence between shortest and tallest
greatestdifference = tallestheight - shortestheight;
Debug.Log(greatestdifference);
// can biuld?
if(greatestdifference > maxDifference)
{
MenuSetup.CanBiuld = false;
transform.GetChild(0).gameObject.GetComponent<Projector>().material = MenuSetup.RedGhost;
}
else
{
MenuSetup.CanBiuld = true;
transform.GetChild(0).gameObject.GetComponent<Projector>().material = MenuSetup.GreenGhost;
}
}
else
{
MenuSetup.CanBiuld = true;
}
and here is ray casts :
report.png
(278.6 kB)
Comment