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 29, 2021 at 05:18 AM · rigidbody2dclampkinematicrigidbody.moveposition

Trouble with implementing Vector2.ClampMagnitude in the Rigidbody2D.MovePosition function

Hi there,

So I basically have a square in my scene that has a Kinematic Rigidbody2D component added to it that I can move with the left and right arrow keys on my keyboard via the MovePosition function but I want it to collide flush against a wall like shown in this video here: https://youtu.be/4VFgIHQdxZk?t=1

I've tried to write the following code with the Vector2.ClampMagnitude function which you can see in this screenshot: https://ibb.co/mFM99Pb

But this code gives me an error. Basically what I'm trying to say in this code is that if the raycast that is shooting from the square hits the wall then the value of velocity can only be between 0 and the value of hit.distance. This means that when raycast hits the wall the player can continue travelling towards the wall at the same speed that it was before the raycast detection and then when hit.distance equals zero the square will stop flush against the wall.

However when I try to run this code I get this error in the console "No overload for method "ClampMagnitude" takes 3 arguments".

I'm just wondering could someone advise me on what I'm doing wrong?

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
0

Answer by DenisIsDenis · May 29, 2021 at 05:30 AM

In the ClampMagnitude function, only the vector and its maximum length are needed:

 Vector2.ClampMagnitude(velocity, distance);
Comment
Add comment · Show 2 · 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 egonspengler_84 · May 29, 2021 at 08:55 AM 0
Share

Thanks for this.

If I'm writing the following code:

  moveInput = Input.GetAxisRaw("Horizontal");
      Vector2 velocity = Vector2.right * (moveInput * speed);
             
     
             if (hit) {
     
                velocity = Vector2.ClampMagnitude(velocity, hit.distance);
     
             }
     
          
 
     

What am I actually saying with the ClampMagnitude function? Am I saying that if the raycast has hit something then velocity will equal to (1,0) or (-1,0) until hit.distance equals zero at which point velocity will equal (0,0)?

avatar image DenisIsDenis egonspengler_84 · May 29, 2021 at 09:33 AM 0
Share

To stop the player at a distance from the wall, we need to modify the code a bit:

 if (hit) {
     float distance = 1.3f;
     velocity = Vector2.ClampMagnitude(velocity, hit.distance - distance);
 }

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

123 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

Related Questions

How to Clamp X-axis Movement only? 1 Answer

Is Use Full Kinematics Contact bugged in 5.6.1? 0 Answers

Question about raycast collision detection involving Kinematic Rigidbody2D 1 Answer

which is best method for 2d player movement 0 Answers

(2D) Can't use colliders when I have a kinematic rigidbody? 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