- Home /
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.
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.
$$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...
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.
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.
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;
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.
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.
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
Follow this Question
Related Questions
How do I reset a variable? 2 Answers
Ship controls partially working 0 Answers
Variable that decreases after a certain amount of time? 2 Answers