Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Nivasraj · Mar 09, 2017 at 08:29 PM · mathf.clamp

I have a problem with mathf.clamp…how to use it to move left and right??

I have my player using character controller or rigidbody…

if(Input.getbuttondown(keycode.Leftarrow)) { movedirection.x=speed*Time.deltatime;

// in there my player at x=0:now i have to move my player at x=1 when once left arrow is pressed…if again left arrow is pressed +1:someone help with this // }

Comment
Add comment · Show 1
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 TreyH · Mar 09, 2017 at 08:52 PM 0
Share

You wouldn't use $$anonymous$$athf.Clamp() itself to move left and right.

Clamp() keeps takes a number, an upper bound, and a lower bound. It returns either:

  • The number

  • The lower bound (if your number was below this)

  • The upper bound (if your number was above this)

You would use Clamp() to prevent a value from being too large or small prior to assigning to as some position component.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Robert_s_a · Mar 10, 2017 at 09:40 AM

As TreyH said, MathF.Clamp() would be used to limit the motion so it doesn't go beyond a set range. You may still need this if you need to limit the player to specific boundaries. Personally, I use Input.GetAxis("Horizontal") for movement, it's a smoother result than with getbuttondown, unless you specifically want it to jump one step at a time. Here's an example.


 public gameObject MyObj;
 float moveHorizontal;
 float maxLeft = -15.0f;
 float maxRight = 15.0f;
 float moveSpeed = 0.5f;
 void Update()
 {
     // Read the keyboard input ( - left and + right arrows or - A and + D keys)
     moveHorizontal = Input.GetAxis("Horizontal");  

     // adjust the input for your scale and clamp it to your boundaries
     moveHorizontal = Mathf.Clamp(moveHorizontal*moveSpeed*Time.deltatime,maxLeft,maxRight);

     // apply the motion to your object
     MyObj.transform.position = MyObj.transform.position+new Vector3(MoveHorizontal,0,0);
 }

You can use GetButtonDown with leftarrow and also with rightarrow in place of GetAxis if you'd like. GetAxis does handle both directions with one command, however.

You can do the same thing with Input.GetAxis("Vertical") for up and down if you want, and that would go in place of the second 0 in the Vector3.

Or if you're doing 2 dimensions, you'd probably be using a Vector2(x , y) without the third 0 for z.

Hope this helps.

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 TreyH · Mar 10, 2017 at 12:32 PM 0
Share

This will clamp your movement vector, but not the position itself. The frame-wise increment will be bound (the product of moveSpeed, moveHorizontal, and the time delta), but the object being moved is not limited beyond that in this setup.

avatar image Robert_s_a TreyH · Mar 10, 2017 at 04:59 PM 0
Share

Thanks for the correction, TreyH. It was a bit late last night when I posted this, and yeah, I clamped it in the wrong place. One of the hazards of insomniac program$$anonymous$$g, I guess. :P

avatar image
0

Answer by Nivasraj · Mar 10, 2017 at 06:55 AM

Thanks for that…but still i cant understand how to use mathf.clamp with my player…

i used mathf.clamp with my player…but the code not working…can you give me some code !!……………thanks

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 Robert_s_a · Mar 10, 2017 at 05:20 PM 0
Share

I made a mistake in the code above, and used $$anonymous$$athf.Clamp on the calculation for the next step in the movement, not as a limit to the entire range of movement itself. (Thanks TreyH for pointing me in the right direction :)

Take the $$anonymous$$athf.Clamp part out of the line its on, so it becomes: moveHorizontal = moveHorizontal*moveSpeed*Time.deltatime;

and change the line below (which actually moves the player) to: $$anonymous$$yObj.transform.position = new Vector3($$anonymous$$athf.Clamp(($$anonymous$$yObj.transform.position.x+moveHorizontal),maxLeft,maxRight)),0,0);

This puts the clamp on the overall range of motion in the X direction, rather than just on the current step.

Basically $$anonymous$$athf.clamp has nothing to do with movement itself and, $$anonymous$$athf.Clamp(someNumber,$$anonymous$$Value,maxValue); is the same code as:

if (someNumber < $$anonymous$$Vaue) { someNumber = $$anonymous$$Value; } else if (someNumber > maxValue) { someNumber = maxValue; }

Hope this is helping. :)

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

96 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

Related Questions

Problem with mathf.clamp on rigid body without gravity 1 Answer

Error when importing mathematics 2 Answers

Min and Max value? 1 Answer

transform.position assign attempt for 'RCCMainCamera' is not valid. Input position is { NaN, NaN, NaN }. UnityEngine.Transform:set_position(Vector3) 1 Answer

Speed exceeds the clamped value by 0.3? 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