Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 StewedHarry · Aug 11, 2020 at 03:23 PM · rotationpositiondistance

Finding a Vector3 from Quaternion, current Vector3 and Distance

I need to use a custom raycast implementation (from the MLAagents framework) and want to find the Vector3 location of where the raycast has hit. The information I have is the Quaternion rotation of the gameobject transmitting the ray, this gameobject's Vector3, and the distance between where the raycast has hit and this gameobject. I'm struggling to find a solution.

On another answer I found this:

 public static Vector3 RayCastHitLocation(Quaternion rotation,Vector3 position, float distance)
      {
          Vector3 direction = rotation * Vector3.forward;
          return position + (direction  * distance);
      }

However, as I increase the distance from a hit object, the resulting Vector3 changes, which shouldn't occur as the raycast is still fixed on the same spot.

Would appreciate any help. Thanks

Comment
Add comment · Show 7
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 Owen-Reynolds · Aug 11, 2020 at 03:48 PM 0
Share

The lines you have are standard and correct. Something else must be wrong. In theory being further away could introduce larger "rounding" errors, but they'd be too tiny to notice.

avatar image davidcox70 Owen-Reynolds · Aug 11, 2020 at 05:03 PM 0
Share

I agree with @Owen-Reynolds. Is it possible that the rotation is relative to the raycasting gameObject, in which case it perhaps shouldn't be Vector3.forward but rather the raycasting GameObject's forward transform?

avatar image StewedHarry davidcox70 · Aug 11, 2020 at 05:06 PM 0
Share

Thanks for the replies! How would I get the gameobject's forward transform?

Show more comments
avatar image StewedHarry · Aug 11, 2020 at 05:17 PM 0
Share

Here is a more fleshed out example of the code

 public class TestScript
     {
 
         public void GetSensorStuff()
         {
             
             // this is a GetComponent<> in the real code
             // RayPerceptionSensorComponent attached to child gameobject of agent gameobject
             // This object is rotated to move sensor around
             RayPerceptionSensorComponent3D rayComponent = new RayPerceptionSensorComponent3D();
         
             var lengthOfRayOutputs = RayPerceptionSensor
                 .Perceive(rayComponent.GetRayPerceptionInput())
                 .RayOutputs
                 .Length;
             
             var rayDistances = new float[5];
             
             List<float[]> rayBuffers = new List<float[]>()
             {
                 new float[(2 + 2) * lengthOfRayOutputs],
                 new float[(2 + 2) * lengthOfRayOutputs],
                 new float[(2 + 2) * lengthOfRayOutputs],
                 new float[(2 + 2) * lengthOfRayOutputs],
                 new float[(2 + 2) * lengthOfRayOutputs]
             };
 
             var rayOutputs = RayPerceptionSensor
                 .Perceive(rayComponent.GetRayPerceptionInput())
                 .RayOutputs;
                 
             for (int i = 0; i < 5; i++)
                 rayOutputs[i].ToFloatArray(2, 0, rayBuffers[i]);
             
             // add just the distances to a new float array which represents all the ray cast distances
             var distances1 = rayBuffers[0][3];
             var distances2 = rayBuffers[1][3];
             var distances3 = rayBuffers[2][3];
             var distances4 = rayBuffers[3][3];
             var distances5 = rayBuffers[4][3];
             
             // I want to convert these distances into Vector3's
             // assu$$anonymous$$g this script is attached to gameobject the rays are cast from
             RayCastHitLocation(
                 transform.rotation,
                 transform.position,
                 distances1.ReverseNormalise(rayComponent.RayLength)
             );
             
         }
         
         public static Vector3 RayCastHitLocation(Quaternion rotation, Vector3 position, float distance)
         {
             Vector3 direction = rotation * Vector3.forward;
             return position - (direction  * distance);
         }
         
     }
avatar image Owen-Reynolds StewedHarry · Aug 12, 2020 at 05:30 PM 0
Share

That's a lot of code, so a lot of things to check (if you know what values they should have -- I sure don't). But at the end, the call to RayCastHitLocation appears to do nothing since there's no "x=" in front. In other words, you don't even even use RayastHitLocation, so it can't be the problem.

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

193 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

Related Questions

How to make Camera position Independent of its Rotation? 1 Answer

Lerp in direction to certain distance 3 Answers

How can i place subjects in different positions? 0 Answers

Make object appear within camera view 0 Answers

Moving child and parent 1 Answer


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