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 07, 2020 at 02:50 PM · physicsraycast

Help with raycast stopping player from going through walls

Hello I am using raycasts to stop the player from going through walls. I have a camera controller that allows the player to move around the 3d space similar to the way you can move around in the scene of a unity project. The method I use to stop the player from going through walls if they move forwards works perfect every time. I tried to use this same method when the player pans left or right. It works some of the time but I can still break through walls alot of the time. I can't figure out why this isn't working like my other raycast method any help would be appreciated. To activate that movement to pan left or right I have it set up so the user just needs to hold the middle scroll button down. Here is the raycast code for the left and right pan.

          //if the middle button is pressed down
         if (horizontalTranslation.isActivated())
         {
             //this code is what moves the camera
             float translateX = Input.GetAxis(mouseHorizontalAxisName) * horizontalTranslation.sensitivity;
             transform.Translate(translateX, 0, 0);
             //variables for the raycast
             origin = transform.position;
             Vector3 eLeft = GetComponent<Camera>().transform.right;
             Vector3 eRight = GetComponent<Camera>().transform.right;
             RaycastHit hitLeft;
             RaycastHit hitRight;
         //raycast that checks left side if there is a hit close enough it loads the last saved 
         //position instead of moving in the direction the user wants to move
         if (Physics.SphereCast(origin, sphereRadius, eLeft, out hitLeft, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
         {
             //checks if raycast hit is in between 1 and 5
             Debug.LogWarning("left initial hit " + hitLeft.distance);
             if ((hitLeft.distance > 1) && (hitLeft.distance < 5))
             {
                 originalRayPos = transform.position;
                 originalRayRot = transform.rotation;
                 Debug.LogWarning("left set " + hitLeft.distance);
             }
             //if raycast hit is less than 1 the last saved position is used to stop player from going that direction
             if (hitLeft.distance < 1)
             {
                 transform.position = originalRayPos;
                 transform.rotation = originalRayRot;
                 Debug.LogWarning("left use " + hitLeft.distance);
             }
         }
         //raycast to check the right side if there is a hit close enough it loads the last saved 
         //position instead of moving in the direction the user wants to move
         if (Physics.SphereCast(origin, sphereRadius, eRight, out hitRight, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
         {
             Debug.LogWarning("left initial hit " + hitRight.distance);
             if ((hitRight.distance > 1) && (hitRight.distance < 5))
             {
                 originalRayPos = transform.position;
                 originalRayRot = transform.rotation;
                 Debug.LogWarning("left set " + hitRight.distance);
             }
             if (hitRight.distance < 1)
             {
                 transform.position = originalRayPos;
                 transform.rotation = originalRayRot;
                 Debug.LogWarning("left use " + hitRight.distance);
             }
         }
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 07, 2020 at 02:56 PM 0
Share

For reference I am posting the other raycast code here in a comment under my post. This code works perfect and isn't buggy at all. I don't know why this works and the code above this is buggy.

     if (scroll.isActivated())
     {
             //this will move the camera forward
             float translateZ = Input.GetAxis(scrollAxisName) * scroll.sensitivity;
             transform.Translate(0, 0, translateZ);
             //variables for the raycasting
             Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             //sends raycast out and returns hit if collision detected
             if (Physics.Raycast(ray, out hit))
             {
             //checks if raycast hit is less than 1 and more than 5 and sets the position varaible with the last position
                 if ((hit.distance < 5) && (hit.distance > 1))
                 {
                     originalRayPos = transform.position;
                     originalRayRot = transform.rotation;                               
                 }
                 //if hit is more than 1 then the last saved position is loaded ins$$anonymous$$d of moving forward
                 if(hit.distance < 1)
                 {
                     transform.position = originalRayPos;
                     transform.rotation = originalRayRot;
                 }
             }

     }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrewu331 · Apr 09, 2020 at 02:53 PM

If anyone else is struggling with this issue I found a better solution heres my code.

     if (horizontalTranslation.isActivated())
     {
         //sets translate to be used for transformation
         float translateX = Input.GetAxis(mouseHorizontalAxisName) * horizontalTranslation.sensitivity;
         //variables for raycasting
         origin = transform.position;
         Vector3 eLeft = -GetComponent<Camera>().transform.right;
         Vector3 eRight = GetComponent<Camera>().transform.right;
         RaycastHit hitLeft;
         RaycastHit hitRight;
         //raycast sent to right
         if (translateX > 0)
         {
             if (Physics.SphereCast(origin, sphereRadius, eRight, out hitRight, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
             {
                 //do nothing if hit is detected
             }
             else
             {
                 //transform position if no hit detected
                 transform.Translate(translateX, 0, 0);
             }
         }
         //raycast sent to left
         if (translateX < 0)
         {
             if (Physics.SphereCast(origin, sphereRadius, eLeft, out hitLeft, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
             {
                 //transform.Translate(translateX, 0, 0);
             }
             else
             {
                 transform.Translate(translateX, 0, 0);
             }
         }    
     }
Comment
Add comment · 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

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

312 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

Related Questions

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

cant get raycast to work with tags 1 Answer

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

Raycast only occurs on start 2 Answers

Adding MaxDistance to RayCast Stops Raycast From working 0 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