Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · May 20, 2019 at 12:12 AM · scripting problemvector3raycastingaverage

I can't average vectors

Hello there, I have a problem.

Video here.

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 ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · May 20, 2019 at 09:10 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

204 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

animation problem 1 Answer

How Do I See My RayCast? 1 Answer

how do i move ludo token to right side 0 Answers

Why does this script check for raycast collision? Is it necessary? 1 Answer

Get point in plane where the normal crosses a GameObject in local space 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges