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 LunguM · Jul 07, 2013 at 01:54 PM · variablecontrolsresetspaceshipdecrease

Variable not decreasing during runtime (C#)

Greetings,

I am experimenting with controls for a space ship and after completing the most basic of controls I decided to put limits on the ship's speed and hit a problem I can't seem to find a solution to.

When moving forward I have a variable A that is compared to a variable B and as long as A is less than B then A will increase and the speed of the ship will increase.

The problem I'm experiencing is that when I move in the opposite direction (backwards) I use the same variable A and instead of the number decreasing it would just place a "-" in front of it.

So, if the object moves forward it will gain a speed of +5.5, when it moves backwards instead of slowly decreasing the number it will get a value of -5.5 which will increase normally. The same thing happens in reverse from this point on.

Below is the code:

 if(Input.GetButton("W") && currentSpeed < topSpeed){
     currentSpeed = Time.time + move;
     _cachedRigidbody.AddForce(Vector3.forward * currentSpeed * move * _cachedRigidbody.mass);
 }
 if(Input.GetButton("S")){
     currentSpeed = -Time.time + move;
     _cachedRigidbody.AddForce(Vector3.back * -currentSpeed * move * _cachedRigidbody.mass);
 }

Any help would be appreciated.

Thank you.

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 supercouge · Jul 07, 2013 at 02:03 PM 0
Share

I'm not sure to have understand everything, but why did you do :

  currentSpeed = -Time.time + move;
 _cachedRigidbody.AddForce(Vector3.back * -currentSpeed * move * _cachedRigidbody.mass);

Ins$$anonymous$$d of:

      currentSpeed = Time.time + move;
     _cachedRigidbody.AddForce(Vector3.back * currentSpeed * move * _cachedRigidbody.mass);

? Also, did you look at $$anonymous$$athf.Abs() ? It may help you.

avatar image LunguM · Jul 07, 2013 at 02:11 PM 0
Share

$$anonymous$$ostly for the Editor but also for when I want to set a stop limit to how much speed it can have when moving backwards.

Without the two "-" the variable would increase when "S" is pressed, even though it would move backwards. With "-" in front, the variable in the Editor would decrease to a -X value.

Also, it would be a bit redundant since the value would increase to the limit set by "topSpeed" and the ship would be unable to increase its speed forward or backwards anymore

Edit: Even with $$anonymous$$ath.Abs() the value would just jump between its positive and negative counterpart...

avatar image D4rt · Jul 07, 2013 at 02:30 PM 0
Share

It's also strange that you use the mass in the force calculation. The physics engine uses the mass when sorting out the forces so there is no need for you to do it.

avatar image LunguM · Jul 07, 2013 at 03:01 PM 0
Share

I removed the mass out of calculation and fiddled around with the code.

If I auto-increment "currentSpeed" (and auto-decrement) the values would go up and down normally, but the ship will continue to increase its speed as long as "currentSpeed" is > 0 or < 0.

So if "currentSpeed" reaches a value of 50 and I press "S", the ship will increase its speed until the value of at least -1 is reached and only then decrease its speed.

1 Reply

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

Answer by robertbu · Jul 07, 2013 at 03:01 PM

Since you are not setting the velocity but instead are using AddForce, 'currentSpeed' is not the current speed. It is the amount of force being applied. Based on your code above and you saying you want to limit 'Z', I'm assuming movement towards positive 'Z' on the world axes is 'backward'. If I can assuming there are no collisions and the direction of your ships movement will always be aligned with the world axes, you can do this:

 var v3 = _cachedRigidbody.velocity;
 if (v3.z > topSpeed)
     v3.z = topSpeed;
 _cachedRigidbody.velocity = v3;

   
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 LunguM · Jul 07, 2013 at 04:13 PM 0
Share

I'm not sure I understood...

Based on what you wrote I changed AddForce to velocity, however when I go into negative, ins$$anonymous$$d of moving backwards, the ship will move forward.

avatar image robertbu · Jul 07, 2013 at 05:32 PM 0
Share

No, don't change your existing code. Just add this code. AddForce() speeds something up or slows it down, and in the process may change the direction. But the underlying speed and direction is the velocity. The use of AddForce() to move something is great. You just need to deal with Rigidbody.velocity when you want to limit the speed.

avatar image LunguM · Jul 09, 2013 at 07:06 PM 0
Share

I apologize for the late reply, only now got the chance to work on my code again.

It works as you stated, the value increases and decreases normally. The only problem is that once the top or lower value is reached the number will no longer increase or decrease.

But I will solve it eventually. Thank you for your help robert :)

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

17 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

Related Questions

How do I reset a variable? 2 Answers

I have a variable that does not re-set on the closing of my program, the variable is modified on the trigger enter, why does it not re-set? 0 Answers

Ship controls partially working 0 Answers

Variable that decreases after a certain amount of time? 2 Answers

1 Variable Affected By Multiple Variables 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