Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by TRAUHR · Mar 08, 2020 at 04:13 PM · unity 2dmathf.clamp

why is my player sticking to mathf.clamp?

Hello, I added to my player jump script a line of code which uses mathf.clamp so that my player does not leave the screen. It works very well ... But my player remains sticking above or below the screen when it touches this limit and I would just like my player to be blocked and not stick. Can you help me? Below you will find my script so you can see my problem.

 void Update () {
  
 transform.position = new Vector3(Mathf.Clamp(transform.position.x, -2.6f, 2.6f), Mathf.Clamp(transform.position.y, -4.8f, 4.8f), transform.position.z);
  
    if (Input.GetKeyDown(UnityEngine.KeyCode.Space) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
          {
              this.GetComponent<Rigidbody2D>().AddForce(ForceSaut);
              audioSource.PlayOneShot(JumpSound);
          }
  }



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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TRAUHR · Mar 09, 2020 at 05:40 PM

here is the final code that works perfectly!

     public Rigidbody2D rb;
 
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update () {
 
         if (rb.position.y < -4.8f)
         {
             rb.position = new Vector2 (0f, -4.8f);
             rb.velocity = new Vector2(0,0);
         }
 
         else if(rb.position.y > 4.8f)
         {
             rb.position = new Vector2(0f, 4.8f);
             rb.velocity = new Vector2(0,0) ;
         }
       
         if (Input.GetKeyDown(UnityEngine.KeyCode.Space) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
         {
             rb.AddForce(ForceSaut);
         }
 }
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
avatar image
0

Answer by streeetwalker · Mar 08, 2020 at 05:12 PM

Hi TrauhrGames ,

the force is still being added through the rigidbody and you are mixing the physics system with direct manipulation of the transform. Try clamping the rigidbody position instead.

file:///Applications/Unity/Hub/Editor/2019.2.11f1/Unity.app/Contents/Documentation/en/ScriptReference/Rigidbody-position.html

Comment
Add comment · Show 13 · 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 TRAUHR · Mar 08, 2020 at 05:22 PM 0
Share

@streetwalker thank you for your answer... But how should i do to clamp the rigidbody position?

avatar image streeetwalker TRAUHR · Mar 08, 2020 at 05:40 PM 0
Share

probably best to create a shortcut "cache" variable to the rigidbody component in the Start method because you are accessing it multiple times:

 private RigidBody rb;
 
 void Start() {
        rb = GetComponent<Rigidbody2D>();
 } 
 
 // then use rb in your update function
 
 // then use rb.position.x  where you do the clamp
 //  ins$$anonymous$$d of transform.position.x
 //
 // same for addforce   rb.addForce ...

does that help?

avatar image TRAUHR streeetwalker · Mar 08, 2020 at 05:56 PM 0
Share

here I changed my script as you advised me, well I think ... because the problem is still present.

    public Rigidbody2D rb;
 
     void Start()
     {
         audioSource = GetComponent<AudioSource>();
         rb = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update () {
 
 
         transform.position = new Vector3($$anonymous$$athf.Clamp(rb.position.x, -2.6f, 2.6f), $$anonymous$$athf.Clamp(rb.position.y, -4.8f, 4.8f), transform.position.z);
 
         if (Input.Get$$anonymous$$eyDown(UnityEngine.$$anonymous$$eyCode.Space) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
         {
             rb.AddForce(ForceSaut);
             audioSource.PlayOneShot(JumpSound);
         }
 }
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

206 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

Related Questions

Exclude objects from Post Processing 1 Answer

Enemys velocity in 2D platformer 0 Answers

Implement a shoot button for Android Device 0 Answers

Physics.OverlapSphere is not detecting any 2D Box Colliders even though there are! 0 Answers

displaying images in canvas problem 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