Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 egonspengler_84 · May 25, 2021 at 01:30 PM · raycastcollision2dboundsmathf.clamp

Trouble with using Mathf.Clamp function in order to create collision detection with raycasts

Hi there,

So I have a scene with a square that I can move left and right with the arrow keys on my keyboard and there are two raycasts that shoot from the bottom right and bottom left corners respectively to detect walls on either side of the square. By using the Mathf.Clamp function I have been able to successfully get the square to collide correctly with the wall on the right side but I can't seem to figure out how to do the same with the wall on the left side.

Here is a video of the issue that you can see here: [MEDIA=youtube]dzcP-huxI54[/MEDIA]

Here is the code that I've written for the square game object:

 public class SquareThree : MonoBehaviour
 {
   
     private BoxCollider2D _col2D;
     public LayerMask _collisionMask;
     private Vector2 botRight;
     private Vector2 botLeft;
     private float rayLength = 1f;
     public float moveInput;
     public float speed = 0.1f;
 
   
     // Start is called before the first frame update
     void Start()
     {
         _col2D = GetComponent<BoxCollider2D>();
     }
 
 
     // Update is called once per frame
     void FixedUpdate()
     {
 
         moveInput = Input.GetAxisRaw("Horizontal");
 
         Bounds bounds = _col2D.bounds;
         botRight = new Vector2(bounds.max.x,bounds.min.y);
         botLeft = new Vector2(bounds.min.x,bounds.min.y);
 
         RaycastHit2D hitR = Physics2D.Raycast(botRight,Vector2.right,rayLength,_collisionMask);
         Debug.DrawRay(botRight,Vector2.right * rayLength, Color.red);
 
         RaycastHit2D hitL = Physics2D.Raycast(botLeft,Vector2.left,rayLength,_collisionMask);
         Debug.DrawRay(botLeft,Vector2.left * rayLength, Color.red);
 
  
 
         if(hitR){
 
       
             transform.Translate( Mathf.Clamp(hitR.distance,0,moveInput * speed),0,0);
 
          
         }else if(hitL){
 
              
              transform.Translate( Mathf.Clamp(-hitR.distance,moveInput * speed,0),0,0);
               
  
         }else{
 
             transform.Translate(moveInput * speed,0,0);
 
         }
  
           
     
      
     }//end of fixed update
 
     
 
  
  
 
 }


If anyone has any suggestions as to what I'm doing wrong I would appreciate it very much. 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

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

167 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

Related Questions

Find closest edge of mesh renderer from raycasthit 1 Answer

Can I use a raycast to retrieve information from the collider? 2 Answers

Checking if ground is 'level' 1 Answer

Simple example bound box or get the top position gameobject 1 Answer

Get closest point on collider on at a certain z depth 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