Hello there, I have a problem.
[Video here.][1]
I am coding a wheel collider with multiple raycasts per wheel. Each ray comes from the center of the wheel and going at a different angle. The rays are the green lines in the scene view. When the rays hit an object, the depth of the ray into the object is calculated and rendered with a magenta line. Finally, I’m trying to average the magenta rays into one ray that symbolises the ground surface’s normal direction (blue line).
My problem is that no matter the angle of the ground, the blue ray always have the same length and always point straight down. Here’s what my code looks like :
void FixedUpdate() {
RaycastHit[] hit = new RaycastHit[4 * (tireResolution + 1)];
Vector3[] tireContact = new Vector3[4 * (tireResolution + 1)];
Vector3[] tireContactDepth = new Vector3[4 * (tireResolution + 1)];
Vector3[] averageContact = new Vector3[4 * (tireResolution + 1)];
for (int i = 0; i < 4; i++)
{
averageContact *= Vector3.zero;*
for (int j = 0; j <= tireResolution; j++)
{
tireContact[i + j] = wheel.transform.TransformDirection(Vector3.forward) * Mathf.Cos(j * Mathf.PI / tireResolution) + wheel_.transform.TransformDirection(Vector3.down) * Mathf.Sin(j * Mathf.PI / tireResolution);
averageContact += tireContact[i + j];
if(Physics.Raycast(wheel*.transform.position, tireContact[i + j], out hit[i + j], wheelRadius))*
{
tireContactDepth[i + j] = tireContact[i + j] * wheelRadius - hit[i + j].distance * tireContact[i + j].normalized;
}
averageContact += tireContactDepth[i + j];
}
averageContact /= tireResolution + 1;
}
}
Where “tireResolution” is the number of rays per wheel.
So, what am I doing wrong ?
P.S :
So, I just commented out this line :
averageContact += tireContact[i +j];
since I was apparently performing the sum twice and with the wrong variable, but now the blue ray doesn’t even appear. What should I do ?
*[1]: https://gfycat.com/JoyfulOblongBuckeyebutterfly*_