- Home /

# I can't average vectors

Hello there, I have a problem.

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[i] = Vector3.zero;
for (int j = 0; j <= tireResolution; j++)
{
tireContact[i + j] = wheel[i].transform.TransformDirection(Vector3.forward) * Mathf.Cos(j * Mathf.PI / tireResolution) + wheel[i].transform.TransformDirection(Vector3.down) * Mathf.Sin(j * Mathf.PI / tireResolution);
averageContact[i] += tireContact[i + j];
if(Physics.Raycast(wheel[i].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[i] += tireContactDepth[i + j];
}
averageContact[i] /= 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[i] += 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 ?

**Answer** by Bunny83
·
May 20, 2019 at 03:44 AM

It looks like you messed up your jagged array indices. You just added up the "i" and "j" index which gives you pretty much nonsense. For example i==0 and j==1 will give you the same result as i==1 and j==0. You probably wanted to multiply one of the variables with a stride factor.

I would generally recommend to calculate the stride once and store it in a variable. Currently it seems you calculate it all the time on the fly at various places.

Thank you, I just replaced [i + j] with [(tireResolution + 1) * i + j] And now the average vector follows the road surface!

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

My player doesn't turn based on the mouse when it's supposed to 0 Answers

Ray origin moving with player movement and mouse movement 2 Answers

How would i go about comparing two transform scales 1 Answer

Raycast detects box collider, but not capsule collider 0 Answers

Boxcast vs Raycast Oddities 1 Answer