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 LunaD03 · Jun 26, 2020 at 04:21 PM · physicsvector3raycasthitphysics.raycast

Raycasts in opposite directions only returning RaycastHits on right

Hello,

I have a wallrunning script which fires Raycasts directly left, directly right, and some diagonals. Upon hitting a wall, that Hit is returned as RaycastHit wallrunWall;, and, should no wall be detected, wallrunWall = new RaycastHit();, as shown here (removed some things that shouldn't matter, and is in FixedUpdate):

 if ( (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out wallrunWall, WallRunDetectionRange) ||
             Physics.Raycast(transform.position, transform.TransformDirection(RaycastLeftDiagonal.normalized), out wallrunWall, WallRunDetectionRange)) && testingForWallRunLeft) {
             
             wallrunningLeft = true;
 
         } else {
             
             wallrunWall = new RaycastHit();
 
             wallrunningLeft = false;
 
         }
         
         
         if ( (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out wallrunWall, WallRunDetectionRange) ||
             Physics.Raycast(transform.position, transform.TransformDirection(RaycastRightDiagonal.normalized), out wallrunWall, WallRunDetectionRange)) && testingForWallRunRight) {
 
             wallrunningRight = true;
 
         } else {
 
             wallrunWall = new RaycastHit();
 
             wallrunningRight = false;
 
         } 

Here, when Debug.Log(wallrunWall.normal); for both left and right sides, I receive the proper normal vectors. However, in my Update method, where I calculate the parallel to the wall for wallrunning (shown here), wallrunWall is correct on the right side, but undefined on the left side.

 inputVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
 
         // CHANGE VELOCITY BY MOVEMENT INPUT
 
         if (!wallrunningLeft && !wallrunningRight) {
             
             velocity = new Vector3(inputVector.x * PlayerMoveSpeed, velocity.y, inputVector.z * PlayerMoveSpeed);
 
         } else {
             
             // weird vector stuff
 
             wallParallel = Vector3.Cross(wallrunWall.normal, Vector3.up);
             
             if (wallrunningRight) {
                 
                 tempVelocity = new Vector3(wallParallel.x * -inputVector.z * PlayerWallrunSpeed, 0, wallParallel.z * -inputVector.z * PlayerWallrunSpeed);
 
             } else {
                 
                 tempVelocity = new Vector3(wallParallel.x * inputVector.z * PlayerWallrunSpeed, 0, wallParallel.z * inputVector.z * PlayerWallrunSpeed);
 
             }
 
 
             velocity = transform.InverseTransformDirection(tempVelocity);
 
         }

The result effect is that wallrunning works perfectly on the right side, but the player doesn't move when on the left side. I've been stuck on this for a while, and any help is greatly appreciated! Thanks :)

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
2
Best Answer

Answer by MichaI · Jun 26, 2020 at 06:04 PM

Hi, It is becaouse after making RaycastHit for the left side and storing it in wallrunWall variable, you are overriding it in next if statement for the right side. So wallrunWall can only be either RaycastHit for right side or empty new RaycastHit(). To avoid it you can store them in two separate vairables or change last "else" to "else if(!wallrunningLeft) ".

  if ( (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out wallrunWall, WallRunDetectionRange) ||
              Physics.Raycast(transform.position, transform.TransformDirection(RaycastLeftDiagonal.normalized), out wallrunWall, WallRunDetectionRange)) && testingForWallRunLeft) {
              
              wallrunningLeft = true;
  
          } else {
              
              wallrunWall = new RaycastHit();
  
              wallrunningLeft = false;
  
          }
          
          
          if ( (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out wallrunWall, WallRunDetectionRange) ||
              Physics.Raycast(transform.position, transform.TransformDirection(RaycastRightDiagonal.normalized), out wallrunWall, WallRunDetectionRange)) && testingForWallRunRight) {
  
              wallrunningRight = true;
  
          } else if(!wallrunningLeft) {
  
              wallrunWall = new RaycastHit();
  
              wallrunningRight = false;
  
          } 



or shorter:

 wallrunningLeft  = (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out wallrunWall, WallRunDetectionRange) ||
              Physics.Raycast(transform.position, transform.TransformDirection(RaycastLeftDiagonal.normalized), out wallrunWall, WallRunDetectionRange)) && testingForWallRunLeft;
          
 if(!wallrunningLeft) {
          wallrunningRight = (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out wallrunWall, WallRunDetectionRange) ||
              Physics.Raycast(transform.position, transform.TransformDirection(RaycastRightDiagonal.normalized), out wallrunWall, WallRunDetectionRange)) && testingForWallRunRight;
 }
 
 if (!wallrunningLeft && ! wallruningRight) {
              wallrunWall = new RaycastHit();
          } 


Ideally you would want to separate your code into separate functions for easier future code maintenance. Also try to have as few member variables as possible. ;)

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 LunaD03 · Jun 27, 2020 at 12:14 AM 0
Share

Aaah this works, thank you! Haha yeah I really need to work on organization

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

235 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

Related Questions

How do I find the closest point on a specific side of a bounding box? 3 Answers

Stop the lasers from keep bouncing 1 Answer

RaycastHit if/else Problem (bug?) 0 Answers

RaycastHit direction and start position 0 Answers

Vector3.Reflect problems 4 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