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 mkjrfan · Nov 21, 2012 at 03:06 AM · movementplatformerzero

How can I make a value not go over or below zero?

I am trying to make a 2d character accelerate and then decelerate back to zero. The problem is the movement is dependent on whether or not the x value is positive or negative. For example for the character to move right the x value must be positive and to go left the x value has to be negative.

When decelerating I subtract the values of x so it goes back to zero. The problem is it works when only going to the right where I can put: if (x<=0) x=0 but how can I make it work for both postive and negative values.

Comment
Add comment · Show 4
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 Kryptos · Nov 21, 2012 at 09:45 AM 0
Share

$$anonymous$$athf.Clamp ?

avatar image mkjrfan · Nov 22, 2012 at 12:00 AM 0
Share

$$anonymous$$athf.Clamp won't help because the speed needs to slowly decrease so the player will slightly move slower when slowing down. Also the x values will go past zero since it is going to increase in speed.

avatar image IT_Criminal · Nov 22, 2012 at 01:53 PM 0
Share

Vector2.Lerp ?

avatar image mkjrfan · Nov 22, 2012 at 04:02 PM 0
Share

Can you explain how Lerp would be useful? I only know that lerp can be used to find an average between 2 numbers but thats it.

5 Replies

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

Answer by mkjrfan · Nov 22, 2012 at 04:17 PM

After reading comments from others I think I do finally have a solution to my problem.

if (Input.GetButton("Horizontal")) { // Accelerating when running if (Input.GetButton("Run")) { speed+=(((walkSpeed*acceleration))*Time.deltaTime*4)*Input.GetAxis("Horizontal"); }

else { speed=(walkSpeed)*Input.GetAxis("Horizontal"); }

} // Slows down when not pressing buttons speed-=(speed*declerate)*Time.deltaTime;

// Clamps the speed to max speed which is for me 5 so it wont go past 5 and below -5 // The main solution to make it stop at zero was rounding it, But it still leaves the problem of sliding at a constant pace for a short period of time. //antiBumbFactor is just used for going down slopes moveDirection = Vector3(Mathf.Clamp(Mathf.Round(speed),-maxSpeed,maxSpeed), -antiBumpFactor, 0);

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
1

Answer by LukaKotar · Nov 24, 2012 at 08:38 AM

I know this has ben marked as answered, but I just wanted to say this:

You can easily check if the value or field - in your case 'float' - is not equal to something with "if(x != 0)" or "if(!x == 0)". The "!" always means "not".

In other words, "x != 0" means "x is not equal to 0". Whenever there is a "!" at the beginning, it means the following is false, so "!x == 0" means "x equals 0 is not true", which basically means the same, but is told in a different way.

Keep that in mind, it might help you later on.

Comment
Add comment · Show 3 · 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 mkjrfan · Nov 24, 2012 at 01:39 PM 0
Share

Thank you for your comment. I did know that ! means not beforehand , but I think you're right and it will come in handy later on.

avatar image MountDoomTeam · Nov 24, 2012 at 02:06 PM 0
Share

if you find the answer then post it.

avatar image sparkzbarca · Nov 24, 2012 at 07:46 PM 0
Share

please note that for small variances in value float comparisions are unreliable.

float comparisons are actually approximated to compare.

avatar image
0

Answer by sparkzbarca · Nov 21, 2012 at 03:09 AM

if(x >= 0) x = 0;

else if (x <= 0) x = 0

also known as

if(decelerating) x = 0

you dont care if its greater or less you just want it at zero :P

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 sparkzbarca · Nov 21, 2012 at 03:14 AM 1
Share

if you want it to be gradual in slowing

deceleration_modifer = .5;

x = x * deceleration_modifer;

if your at -100 it will be

-100 * .5 = -50

if your at 100 it will be

100 * .5 = 50

might want to add a

if (mathf.approximately(x, 0) x = 0;

that way once its really close to 0 it becomes 0

avatar image mkjrfan · Nov 21, 2012 at 03:16 AM 0
Share

The thing is it won't work when moving in an opposite direction of the x value. For example if the player moves left the x value will be less than zero, so by putting if(x<0) x=0; then the character cannot move left because the x value will make it zero since x will be a negative value in order for it to move to the left.

I have tried this already and that is the result.

avatar image sparkzbarca · Nov 21, 2012 at 03:24 AM 0
Share

oh your testing even during acceleration?

anyways my comment answers your question.

dont subtract. $$anonymous$$ultiply so you keep the sign.

avatar image mkjrfan · Nov 21, 2012 at 03:37 AM 0
Share

The whole process I'm trying to achieve is slowly gaining up speed when the running button is pushed and then when nothing is pressed that is when the character would decelerate.

I tried the x=x*deceleration_modifer (which is for me speed= speed*acceleration) and it might also be where I placed the deceleration as well. Should I post what I did?

avatar image sparkzbarca · Nov 21, 2012 at 04:38 AM 1
Share
 acceleration = 2;
 deceleration = .5;
 
 if (run_button_down)
 {
 speed = speed * acceleratoin;
 }
 
 if (run_button_up)
 {
 speed = speed * decleration;
 }
 
 if speed is right so positive
 
 speed = 10;
 
 speed = 10 * .5;
 
 speed = 5;
 
 if speed is left so negative
 
 speed = -10
 
 speed = -10 * .5
 
 speed = -5;
 
 how is this not what you want?


either way positive or negative the number approaches zero

Show more comments
avatar image
0

Answer by IT_Criminal · Nov 23, 2012 at 01:01 PM

earlier i said this was achievable with Lerp ... it is ...

 void Update()
 {
      x = Mathf.Lerp(x,0,0.1F);
 }

the number x would gradualy come closer to zero (even if negative) however the closer to zero it gets... the slower it falls to zero ... i mean ... it goes from -1000 to -1 pretty fast ... but after that the fall-rate it begins to slow down dramatically

but there is an even easier way to do this !!!

Edit->Project Settings->Input

choose the axis that you are using to control your character's x then set the Gravity to something small ... that should do it. this will make the player input fall slowly to neutral ... instead of stopping ... possibly creating the desired effect

Comment
Add comment · Show 4 · 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 mkjrfan · Nov 23, 2012 at 04:34 PM 0
Share

What does .1F mean? Yes I think I am starting to understand how Lerp would help.

avatar image IT_Criminal · Nov 24, 2012 at 08:01 AM 0
Share

ok ... let me explain

in c# you have to put "F" after floats or the compiler won't detect them properly ... that 0.1F is actualy 0.1 ... did you try the input alteration approach ?

avatar image mkjrfan · Nov 24, 2012 at 01:41 PM 0
Share

No I did not but I will try and test it later on. Thank you for your help.

avatar image IT_Criminal · Nov 24, 2012 at 01:43 PM 0
Share

np ... altering control sensitivity/gravity to create acceleration/deceleration seems the best solution to me because it doesn't involve any code

avatar image
0

Answer by MountDoomTeam · Nov 24, 2012 at 08:06 AM

modulo makes an escalator of numbers.

mathf abs returns the value as positive whether it is negative or positive, and you can also get the

Mathf.Sign Returns the sign of f.

so that on the one hand you can use the sign as a condition or as a multiplier, and on the other hand you can make a calculation with just a positive number regardless of its sign. so you can do a calculation on the absolute, and then multiply by the sign, and it gives you a lot of control on your game.

If you still can't understand with all these explanations, you should work at yourself and not look for an answer but test all the options we've given you

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 mkjrfan · Nov 24, 2012 at 01:37 PM 0
Share

If you looked at all the answers you would have found out I did find an answer by myself by using all of the help given to me.

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

16 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

Related Questions

2D jump with Raycast 1 Answer

How Do I Check if another key was pushed while one key was being held down? 0 Answers

Character facing the position of mouse cursor (2d platformer),Sprite changing based on mouse position 1 Answer

Rigidbody Platform Character Movement 0 Answers

while Loop isn't running, or not doing what I expect it to. 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