Fuzzzy
July 8, 2015, 11:18am
1
Hi,
I am following this page :
I am looking at getting the normal of the face my ray is touching (projecting down from my player, will then align the said player to that normal…)
here’s my code :
void Update()
{
RaycastHit hit;
float distanceToGround = 0;
if (Physics.Raycast(transform.position, Vector3.down, out hit))
distanceToGround = hit.distance;
Vector3 incomingVec = hit.point - transform.position;
Vector3 reflectVec = Vector3.Reflect(incomingVec, hit.normal);
Debug.DrawLine(transform.position, hit.point, Color.red);
Debug.DrawRay(hit.point, reflectVec, Color.green);
print(incomingVec);
print("incomingVec");
print(hit.normal);
print("hit.normal");
}
The two debugs don’t display any line whatsoever
and
the results I get from my prints seems totally wrong :
hit.normal = (0.0, 1.0, 0.0) <= will keep being 0 1 0 no matter the surface inclination …
incomingVec = (0.0, -10.2, 0.0)
I was thinking I am maybe sending my ray in the wrong direction so its actually not hitting anything ? Dificult to say , in that case I would expect only 0 0 0 in the hit normal …
Help much appreciated,
thanks !
Duugu
July 8, 2015, 11:40am
2
You’re missing some brackets. That’s for sure. Don’t know if there are other problems.
void Update(){
RaycastHit hit;
float distanceToGround = 0;
if (Physics.Raycast(transform.position, Vector3.down, out hit)){
distanceToGround = hit.distance;
Vector3 incomingVec = hit.point - transform.position;
Vector3 reflectVec = Vector3.Reflect(incomingVec, hit.normal);
Debug.DrawLine(transform.position, hit.point, Color.red);
Debug.DrawRay(hit.point, reflectVec, Color.green);
print(incomingVec);
print("incomingVec");
print(hit.normal);
print("hit.normal");
}
}
Fuzzzy
July 8, 2015, 11:46am
3
Duugu:
You’re missing some brackets. That’s for sure. Don’t know if there are other problems.
void Update(){
RaycastHit hit;
float distanceToGround = 0;
if (Physics.Raycast(transform.position, Vector3.down, out hit)){
distanceToGround = hit.distance;
Vector3 incomingVec = hit.point - transform.position;
Vector3 reflectVec = Vector3.Reflect(incomingVec, hit.normal);
Debug.DrawLine(transform.position, hit.point, Color.red);
Debug.DrawRay(hit.point, reflectVec, Color.green);
print(incomingVec);
print("incomingVec");
print(hit.normal);
print("hit.normal");
}
}
thanks, indeed (weird I didnt get any error…) … but unfortunately the results stays the exact same
Instead of asking “Am I hitting nothing” you might want to ask “Am I hitting myself?” or even “What am I hitting?”
Try checking the ray cast hit.transform in your debug section.
Good luck!
1 Like
Thanks ! indeed looking at the hit.transforms helped me a lot !
I can now align my character to the normal of the hit by calculating the angle between the two vectors (with nice and kind euler.rotations, the quaternion lookat just go crazy…)… a bit rough still (still some work to do for the case the character is not facing the slop, smoothing up the thing)… but I am getting there.
thanks !
Duugu
July 8, 2015, 5:25pm
6
The reason for that is the alternative notation of the IF Statement
if(condition){
//code
//code
//code
}
and
if(condition)
//code
Without any brackets the IF statement applies to the next line. And ONLY to the next line.