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 /
  • Help Room /
avatar image
0
Question by andrewu331 · Apr 01, 2020 at 05:50 PM · cameraphysicsraycast

Cannot figure out how to measure the distance of a raycast being sent out to the side of an object

Basically I have a camera in 3d space that the user controls for a 3d house tour. I have a raycast that prevents the user from going through walls that works good using raycast as long as the wall is in front of the user and they are moving forward. My problem is I want to replicate that when the user moves the camera from side to side so the camera will stop when it gets too close to a wall. I figured out how to send the raycast to the left and right of the camera but I cant get the raycast distance using that method. If anyone knows a way to get the distance or a way in general to get the raycast to prevent the user from going through a wall I would really appreciate it.

This is the raycast I want to make work

         if (horizontalTranslation.isActivated())
         {
             float translateX = Input.GetAxis(mouseHorizontalAxisName) * horizontalTranslation.sensitivity;
             transform.Translate(translateX, 0, 0);
             var lft = transform.TransformDirection(Vector3.left);
             var rgt = transform.TransformDirection(Vector3.right);
              //left raycast
             if (Physics.Raycast(transform.position, lft, 4))
             {
                 originalRayPos = transform.position;
                 originalRayRot = transform.rotation;
                 Debug.LogWarning("left raycast set");
             }
             if (Physics.Raycast(transform.position, lft, 1))
             {
                 transform.position = originalRayPos;
                 transform.rotation = originalRayRot;
                 Debug.LogWarning("left raycast fire");
             }
             //right raycast
             if (Physics.Raycast(transform.position, rgt, 4))
             {
                 originalRayPos = transform.position;
                 originalRayRot = transform.rotation;
                 Debug.LogWarning("right raycast set");
             }
             if (Physics.Raycast(transform.position, rgt, 1))
             {
                 transform.position = originalRayPos;
                 transform.rotation = originalRayRot;
                 Debug.LogWarning("right raycast fire");
             }
         }

This is what I did for the raycast for when the camera moves forward and it works perfect I'm just adding it here for reference because this is what I want to accomplish with the other code

             if (scroll.isActivated())
             {
                 float translateZ = Input.GetAxis(scrollAxisName) * scroll.sensitivity;
                 transform.Translate(0, 0, translateZ);

                 Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
                 RaycastHit hit;
                 if (Physics.Raycast(ray, out hit))
                 {
                     // the object identified by hit.transform was clicked
                     // do whatever you want              
                     if ((hit.distance < 5) && (hit.distance > 1))
                     {
                         originalRayPos = transform.position;
                         originalRayRot = transform.rotation;                               
                     }
                     if(hit.distance < 1)
                     {
                         transform.position = originalRayPos;
                         transform.rotation = originalRayRot;
                     }

                 }

             }
         }      


Comment
Add comment · Show 1
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 andrewu331 · Apr 02, 2020 at 12:57 PM 0
Share

I was able to figure out how to get the distance of the hit but this method isn't working correctly to stop the user from going through the wall if they move from the side. Here is what I have now. It is pretty buggy and doesn't work too great. the camera gets stopped when it shouldn't be stopped. If anyone has an alternative or a method they could point me towards that would be awesome.

         if (horizontalTranslation.isActivated())
         {
             float translateX = Input.GetAxis(mouseHorizontalAxisName) * horizontalTranslation.sensitivity;
             transform.Translate(translateX, 0, 0);

             RaycastHit hitLeft;
             RaycastHit hitRight;            

             var lft = transform.TransformDirection(Vector3.left);
             var rgt = transform.TransformDirection(Vector3.right);
             if (Physics.Raycast(transform.position, lft, out hitLeft))
             {
                 Debug.LogWarning(hitLeft.distance);
                 if((hitLeft.distance < 4) && (hitLeft.distance > 1))
                 {
                     originalRayPos = transform.position;
                     originalRayRot = transform.rotation;
                 }
                 if (hitLeft.distance < 1)
                 {
                     transform.position = originalRayPos;
                     transform.rotation = originalRayRot;
                 }
             }

             if (Physics.Raycast(transform.position, rgt, out hitRight))
             {
                 Debug.LogWarning(hitRight.distance);
                 if ((hitRight.distance < 4) && (hitRight.distance > 1))
                 {
                     originalRayPos = transform.position;
                     originalRayRot = transform.rotation;
                 }
                 if (hitRight.distance < 1)
                 {
                     transform.position = originalRayPos;
                     transform.rotation = originalRayRot;
                 }
             }

         }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by streeetwalker · Apr 02, 2020 at 05:57 PM

Hi @andrewu331, I'm trying to figure out why you can't get this RayCast hit distance when you cast rays from the left or right side of the camera. hit.distance is the distance you need, just like the RayCast you did using ScreenToPointRay, so where was the problem? What method were you using?

OK, here is a simple solution that does both directions in one shot. I've tested this on paper, and it works given the assumptions:

   // assumes player moves right or left parallel to world x
  // get the direction and magnitude of the move
  // use those to determine if player can move
  // uses a padding value to prevent movement too close 
  //    to an obstacle
 float translateX = Input.GetAxis(mouseHorizontalAxisName) * horizontalTranslation.sensitivity;
 
 float magnitudeX = Math.Abs( translateX ); // how far?
 if( magnitudeX > 0 ) { // if moving
      float direction  = Mathf.Sign( translateX ); // left or right?
      float padding = 1f; // give me some room!
      Vector3 rayCastDir = Vector3.right * direction; // ray direction
      if ( Physics.Raycast( transform.position, rayCastDir, out hit ) ) {
         float distance = hit.distance - padding; // apply padding
         if( distance > 0 ) { // there is room to move
             if( distance < magnitudeX ) { // translateX moves too far
                   // move close as possible
                   translateX = distance * direction; 
             }
         } else translateX = 0; // no room to move
     }
     transform.position = transform.position + new Vector3( translateX, 0, 0 );
 }

Comment
Add comment · Show 4 · 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 andrewu331 · Apr 03, 2020 at 12:38 PM 0
Share

I figured out how to get the distance I ended up posting a reply with the new code under my original post. The problem now is the raycast is hitting stuff that isn't there so I think I don't have it set up right.

avatar image streeetwalker andrewu331 · Apr 03, 2020 at 01:34 PM 0
Share

@@andrewu331, OK, I looked at your new code, but coulple questions first: 1. are you always moving left or right parallel with the world x axis, or the player can be at an angle and you want to move aligned with the players local x axis? 2. is the camera tied to the player?

avatar image andrewu331 streeetwalker · Apr 06, 2020 at 02:57 PM 0
Share

@streeetwalker The player can be at any angle and I want to move aligned with the players local x axis and the player is actually moving the camera itself. Sorry for the late response.

Show more comments

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

354 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Why is my Physics.Raycast not returning true? 0 Answers

Cone 'Line of Sight' will not trigger on collision 0 Answers

Ray cast to object to transform, instead all objects with script transform. 0 Answers

Raycast from camera only working from certain angles 1 Answer

cant get raycast to work with tags 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