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 BobbleHead · Jan 20, 2013 at 05:59 PM · mathfclamp

Clamping vertical position help

Hello all; I'm addinga gravitatonal force to my player, but I wish to clamp the player from it's position as of when gravity was turned on, and that position.y+15 for example.

Here's what I've got; it doesn't work..

 function Update(){
 
     if(Input.GetKeyDown(KeyCode.M) && timer>0.5){
         InvertGravity();
         }
 
 if(Gravity == true){
         transform.position = Vector3(transform.position.x, Mathf.Clamp(0, currentpos.y, currentpos.y+12), transform.position.z);
         }
     
     }
     
     function InvertGravity(){
     
         
         currentpos.y = transform.position.y;
         maxpos.y = currentpos.y +10;
     
           if (Gravity == false && timer > 0){
                 Gravity = true;
                 timer -=Time.deltaTime;
                 }
             else if(Gravity == true ){
                 Gravity = false;
                 }
             if (Gravity == true){
                 timer -= Time.deltaTime;
                 Debug.Log("whatat");
                 }
             
         }


Needless to say, i don't quite understand the clamp process =/

Thank you in advance

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

Answer by Owen-Reynolds · Jan 20, 2013 at 11:22 PM

As an alternative, just don't use clamp. Clamp is a teeny, tiny trick that does nothing magical at all. The main reason Unity has it is that people with a math or graphics card background use "clamp" as a technical term. It doesn't even make your program run faster.

All clamp does is a pair of Ifs. Say you want pos.y to never get above cur.y+12, write: if(pos.y>cur.y+12) pos.y=cur.y+12;

If you want to never dip below cur.y-12, write the opposite IF. Clamp is merely those two ifs in a row (maybe with an else.)

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 BobbleHead · Jan 25, 2013 at 09:17 PM 0
Share

Ended up going with this; thanks for your help dude!

avatar image
0

Answer by FL · Jan 20, 2013 at 06:09 PM

Mathf.Clamp(A, B, C) generally returns B. Returns A if A > B. Return C if C < B.

Examples:

Mathf.Clamp(5, 7, 10) returns 7.

Mathf.Clamp(5, 1, 10) returns 5.

Mathf.Clamp(5, 11, 10) returns 10.

Mathf.Clamp(0, currentpos.y, currentpos.y+12) always returns currentpos.y or 0 if currentpos.y<0, but never currentpos.y+12 because currentpos.y+12

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

10 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

Related Questions

How to limit the rotation of an object?? 2 Answers

gesture rotation clamp 2 Answers

MathF clamp not working 1 Answer

Mathf.Clamp is not Clamping. 1 Answer

Swipe movement limitation 2 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