wrong slope on terraint
Hi everyone, I would like to understand in certain points of the terrain what its slope angle is, but I cannot understand the results I attach code (in update function) , screenshots and results
RaycastHit hit;
Ray raggio = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(raggio, out hit, Mathf.Infinity, MyGLB_E.layer_Terreno))
{
float normalizedX, normalizedY; Vector3 normale;
indicatore.transform.position = hit.point;
hPoint = hit.point;
normalizedX = hit.point.x / terrainMesh.terrainData.size.x;
normalizedY = hit.point.z / terrainMesh.terrainData.size.z;
normale = terrainMesh.GetComponent<TerrainCollider>().terrainData.GetInterpolatedNormal(normalizedX, normalizedY);
hpointAngolo = terrainMesh.GetComponent<TerrainCollider>().terrainData.GetSteepness(normalizedX, normalizedY);
hString = "TERRAINDDATA:\n\rSP : " + hpointAngolo.ToString() + "\n\rUP : " + Vector3.Angle(normale, Vector3.up).ToString() + "\n\rDW : " + Vector3.Angle(normale, Vector3.down).ToString() + "\n\rFW : " + Vector3.Angle(normale, Vector3.forward).ToString() + "\n\rBW : " + Vector3.Angle(normale, Vector3.back).ToString() + "\n\rRG : " + Vector3.Angle(normale, Vector3.right).ToString() + "\n\rLF : " + Vector3.Angle(normale, Vector3.left).ToString();
normale = hit.normal;
hString0 = "RAYCAST\n\rUP : " + Vector3.Angle(normale, Vector3.up).ToString() + "\n\rDW : " + Vector3.Angle(normale, Vector3.down).ToString() + "\n\rFW : " + Vector3.Angle(normale, Vector3.forward).ToString() + "\n\rBW : " + Vector3.Angle(normale, Vector3.back).ToString() + "\n\rRG : " + Vector3.Angle(normale, Vector3.right).ToString() + "\n\rLF : " + Vector3.Angle(normale, Vector3.left).ToString();
//Debug.Log(hString0 + hString);
}
Result: X Point left : RAYCAST
UP : 0
DW : 180
FW : 90
BW : 90
RG : 90
LF : 90
TERRAINDDATA:
SP : 0,5237671
UP : 0,5237671
DW : 179,4762
FW : 89,62936
BW : 90,37065
RG : 89,62936
LF : 90,37065
X Point Center RAYCAST
UP : 49,58921
DW : 130,4108
FW : 92,9032
BW : 87,0968
RG : 139,4404
LF : 40,55961
TERRAINDDATA:
SP : 13,1032
UP : 13,10318
DW : 166,8968
FW : 90,73031
BW : 89,2697
RG : 103,0821
LF : 76,9179
X Point Right RAYCAST
UP : 50,68999
DW : 129,31
FW : 92,8371
BW : 87,1629
RG : 39,45312
LF : 140,5469
TERRAINDDATA:
SP : 4,573589
UP : 4,573675
DW : 175,4263
FW : 87,25487
BW : 92,74513
RG : 93,65543
LF : 86,34457
Raycast is more precise of terraindata.. but both are imprecise
Tanks for your help, sorry for the english but i don't speak it
Answer by Alieno79 · Jan 20, 2021 at 01:29 PM
ok, i found problem in get normal...
this is code in update :
RaycastHit hit;
//Ray raggio = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 posiz = new Vector3(36.271f, 11f, 6.72780f);
if (Physics.Raycast(posiz + (Vector3.up*5),Vector3.down, out hit, Mathf.Infinity, MyGLB_E.layer_Terreno))
{
float normalizedX, normalizedY; Vector3 normale;
//indicatore.transform.position = hit.point;
hPoint = hit.point;
normalizedX = posiz.x / terrainMesh.terrainData.size.x;
normalizedY = posiz.z / terrainMesh.terrainData.size.z;
normale = terrainMesh.GetComponent<TerrainCollider>().terrainData.GetInterpolatedNormal(normalizedX, normalizedY);
hpointAngolo = terrainMesh.GetComponent<TerrainCollider>().terrainData.GetSteepness(normalizedX, normalizedY);
hString = "TERRAINDDATA:\n\rSP : " + hpointAngolo.ToString() + "\n\rUP : " + Vector3.Angle(Vector3.up,normale ).ToString() + "\n\rDW : " + Vector3.Angle(normale, Vector3.down).ToString() + "\n\rFW : " + Vector3.Angle(normale, Vector3.forward).ToString() + "\n\rBW : " + Vector3.Angle(normale, Vector3.back).ToString() + "\n\rRG : " + Vector3.Angle(normale, Vector3.right).ToString() + "\n\rLF : " + Vector3.Angle(normale, Vector3.left).ToString() + "\n\rNR : " + normale;
hN = normale;
normale = hit.normal;
hN0 = normale;
hString0 = "RAYCAST\n\rUP : " + Vector3.Angle(normale, Vector3.up).ToString() + "\n\rDW : " + Vector3.Angle(normale, Vector3.down).ToString() + "\n\rFW : " + Vector3.Angle(normale, Vector3.forward).ToString() + "\n\rBW : " + Vector3.Angle(normale, Vector3.back).ToString() + "\n\rRG : " + Vector3.Angle(normale, Vector3.right).ToString() + "\n\rLF : " + Vector3.Angle(normale, Vector3.left).ToString() + "\n\rNR : " + hit.normal;
}
this is code in gizmo :
GUIStyle style = new GUIStyle(GUI.skin.label);
style.alignment = TextAnchor.MiddleLeft;
Handles.Label(hPoint, hString0, style);
Handles.Label(hPoint + Vector3.right, hString, style);
Gizmos.color = Color.green;
Gizmos.DrawLine(hPoint,hPoint + ( hN * 1));
Gizmos.color = Color.red;
Gizmos.DrawLine(hPoint, hPoint + (hN0 * 1));
why normal of terraindata is different to normal of raycast?
Tanks for your help
Your answer
Follow this Question
Related Questions
How to change ray angle of inclination 1 Answer
Wall sliding with characterController against an edge 0 Answers
How do i limet angles of a raycast? 0 Answers
Find a point in space using Vector3 angles and Raycast 1 Answer
Need to place an object at a certain angle form the camera and at a certaine distance 0 Answers