- Home /
I'm not getting helpful answers do to people not actaully reading my whole question or not understanding it.
Insure that the player can't increase speed when over the speed limit
We're working with a space shuttle in zero-G that can only thrust forward.
How do I go about insuring that the player can't just accelerate infinitly but instead can only thrust up to a certain top speed and also without limiting the possible speed the shuttle could end up in (If perhaps it gets hit by an asteroid or something).
I've tried so many things but for some reason the results don't make sense to me. Like for one instance the shuttle didn't thrust past the top speed but when I slightly turned and kind if drifted in space I accelerated way beyond my top speed, only moving to the left but not actually forward. I can kind of guess why it happened but I can't easily comprehend the problem to think of a solution.
Answer by RedDeCipher · Jan 15, 2018 at 11:01 PM
It should be something like this
const float MaxSpeed = *Something*;
const float regularAcceleration = *Acceleration*;
if(velocity >= MaxSpeed){
velocity = MaxSpeed
acceleration = 0;
}else
acceleration = regularAcceleration ;
No. I don't want to clamp the velocity. It's also more complicated than this. I've already tried something like this and the results were terrible.
Oh okay, you can put a time limit something like
const int countdown = 300(for example);
if(countdown > 0){
vellocity += aceleration;
countdown---;
}else
//Don't accelerate
So it wont be clamped but ship will stoped accelerating after a while you can even base the time on other variables.
Oh wait now I get what you are trying to say, You should set a fake velocity that will increase even after acceleration stops like and continue to add that.
const float $$anonymous$$axSpeed = *Something*;
float fakeVelocity;
if(velocity >= $$anonymous$$axSpeed){
velocity = $$anonymous$$axSpeed
fakeVelocity += acceleration;
}else{
velocity += acceleration;
fakeVelocity = velocity;
}
Then use it to calculate force when you collide with something
Answer by Dray · Jan 16, 2018 at 12:16 AM
If you don't want to clamp the velocity, what you need is some kind of friction, even though that doesn't make much sence in space ,heh. The easiest thing to do would probably be to define a friction value between zero and one (~percentage) and multiply your speed with (1 - friction). For example, 10% friction:
float friction = 0.1f; // = 10%
void FixedUpdate() {
speed *= (1 - friction);
}
This will make you lose 10% of your current speed in every physics frame. Make sure you put it in FixedUpdate() and play around with the percentage. You will need to increase your acceleration to counteract the friction
Answer by Ginxx009 · Jan 16, 2018 at 01:11 AM
Well if you want to make sure that your object will not increase over speed limit is that do an if else statement so we can go something like this
float speed = 10.0f;
float speedLimit = 15.0f;
if(speed > speedLimit){
//reduce speed here
}
It's not the actual code okay. So what i am telling you is the logic behind it. Or there's another way of doing it. You can use Mathf.Clamp. Use it on the rigidbody.velocity values after the force is applied to determine speed caps.
Follow this Question
Related Questions
How to reach a specific max speed with AddForce() ? 1 Answer
How do i make the Impulse i give to my Player not be lowered by acceleration of gravity? 1 Answer
AddForce for spaceship acceleration, but with max speed 2 Answers
How to limit the force from player input without limiting speed 2 Answers
Ball Addforce Acceleration Speed 1 Answer