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 Chrisdinho · Mar 14, 2017 at 01:57 PM · physicsraycastingpositioning

How to fix Position relative to Raycast lenghts

Hi, my goal is to get the current Position of an Object in an real Room and visualize that in Unity. I've reconstucted the Room(simple 3x5x2m Room) via Unity. The Object has an tilt sensor and six ultasonic sensors. My Idea was to rotate the Objects similar to the tilt sensor and then get Raycasts in all six directions and compare those to what the actual distance in that direction should be (ultrasonic sensors). Then i'll add those differences to one new Vector 3 and add this to my transform.position. Unfortunately my Object keeps jumping around and is not just fixed to one position.

My Code looks like this

 void Update () {
     //ROTATE DRONE
     transform.rotation = Quaternion.Euler(pitch, compassHeading - configLoaderBehaviourScript.roomRotation, roll);
     //UPDATE DIRECTIONS
     directions[0] = transform.forward;
     directions[1] = -transform.right;
     directions[2] = transform.right;
     directions[3] = -transform.up;
     directions[4] = -transform.forward;
     directions[5] = transform.up;

     List<Color> colors = new List<Color>() {Color.red,Color.blue,Color.green,Color.yellow, Color.white, Color.magenta };
     //ULTRASONIC
     deltaPosition = new Vector3();

     Debug.DrawRay(transform.position, (Vector3)directions[0] * (float)distances[0], colors[0], 2, false);
     Debug.DrawRay(transform.position, (Vector3)directions[1] * (float)distances[1], colors[1], 2, false);
     Debug.DrawRay(transform.position, (Vector3)directions[2] * (float)distances[2], colors[2], 2, false);
     Debug.DrawRay(transform.position, (Vector3)directions[3] * (float)distances[3], colors[3], 2, false);
     Debug.DrawRay(transform.position, (Vector3)directions[4] * (float)distances[4], colors[2], 2, false);
     Debug.DrawRay(transform.position, (Vector3)directions[5] * (float)distances[5], colors[3], 2, false);



     for (int i = 0; i < 6; i++)
     {

             if (((float)distances[i]) >= 0)
             {
                 RaycastHit hit;
                 Ray ray = new Ray(transform.position, (Vector3)directions[i]);
                 if (configLoaderBehaviourScript.debugMode)
                 {
                     Debug.DrawRay(transform.position, (Vector3)directions[i] * (float)distances[i], colors[i], 2, false);
                     createLine(transform.position, (Vector3)directions[i] * (float)distances[i]);
                 }
                 if (Physics.Raycast(ray, out hit))
                 {
                 //30 == 3 Meters => MAxRange of Sensors
                     if ((float)distances[i] < 30)
                     {
                         deltaPosition += (Vector3)directions[i] * (hit.distance - (float)distances[i]);
                     }
                     else if (hit.distance < 30)
                     {
                         deltaPosition += (Vector3)directions[i] * (hit.distance - 30);
                     }

                 }
                 else
                 {
                     //NOHIT -> Not in Room anymmore -> reset position
                     transform.position = new Vector3(0f, 10f, 10f);
                     break;
                 }
         }

     }

     transform.position += deltaPosition;
     [...]
     }


First 2 Frames (The Colours for back and up are wrong here) alt text alt text After that it just seems to move constantly.

Does anyone have an Idea how to fix this?

Best Regards

1.png (103.5 kB)
2.png (106.1 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Physics.Boxcast not working with mesh collider 1 Answer

Non-convex mesh for ray-casting only as child of object with rigid body 0 Answers

Some raycasts don't hit the object 0 Answers

Need help with basic target system 1 Answer

Raycasting not behaving as expected? 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