- Home /
Limited Energy Regeneration
I'm making a Space Sim, and I have a boost script. The problem is, the player can boost for forever so I need to make it so that the boost can only be limited, and the boost starts to regenerate over time when the player isn't using boost. How should I do this with the existing script:
var Speed : int = 50;
var BoostSpeed : int = 200;
var NormalSpeed : int = 50; ///Must be same as Speed int
function Update()
{
transform.Translate(Vector3.forward * Time.deltaTime * Speed);
if (Input.GetKeyDown ("space")){
Speed = BoostSpeed;
}
if (Input.GetKeyDown ("space")){
BoostSpeed = Speed;
}
if (Input.GetKeyUp ("space")){
Speed = NormalSpeed;
}
if (Input.GetKeyUp ("space")){
NormalSpeed = Speed;
}
}
Answer by DayyanSisson · Oct 13, 2011 at 11:09 PM
I pretty much figured this out a while ago, but so this has an answer, here's the script with the limited energy:
var starshipEnergy : float = 500;
var energyLossRate : float = 100;
var energyRegenerationRate : float = 25;
var canBoost : boolean = true;
var boostSpeed : float = 200.0;
var cruisingSpeed : float = 50.0;
private var Speed : float = 50.0;
private var fighter : boolean = false;
transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);
if(!fighter){
energyLossRate = 0;
energyRegenerationRate = 0;
}
if(Input.GetKey("space") && starshipEnergy > 0 && canBoost){
starshipEnergy -= energyLossRate * Time.deltaTime;
}
else if(starshipEnergy <= 500 && canBoost){
starshipEnergy += energyRegenerationRate * Time.deltaTime;
}
if(Input.GetKey("space") && starshipEnergy > 0 && canBoost)
Speed = boostSpeed;
else
Speed = cruisingSpeed;
Hope this helps anyone with the same problem. You might need to change some variables to suit your own needs.
Answer by 3D-Magic-VR · Aug 22, 2011 at 09:09 PM
Hi, try to use this:
var normalSpeed : float;
var turboSpeed : float;
var limitTurboTime : float;
var maxturbo : float;
var regen : boolean;
function Awake () {
limitTurboTime = maxTurbo;
}
function Update () {
If (Input.GetKeyDown("space")) {
normalSpeed = normalSpeed * turboSpeed;
regen = false;
if (limitTurboTime > 0)
limitTurboTime -= Time.deltaTime;
}
if (Input.GetKeyUp("space")) {
normalSpeed = normalSpeed/turboSpeed;
regen = true;
}
if (limitTurbotime < maxTurbo && regen)
limitTurboTime += Time.deltaTime;
}
I think this should do the work.
Have you tried this yourself. First of all there are variable errors. Once you fix those, it doesn't exactly work. The speed doesn't change and the variable go crazy in the inspector.
Answer by Rennat · Aug 23, 2011 at 04:42 AM
One way to achieve this would be to add some new variables:
var CanBoost : boolean = true; // true or false can we boost rignt now?
var BoostAmount : float = 1; // (0 to 1) amount of boost left
var BoostDrainRate : float = 5; // seconds it should take for a full boost to drain
var BoostRechargeRate : float = 10; // seconds it should take to recharge fully from no boost
then instead of using using GetKeyDown
and GetKeyUp
you can set the speed for that frame based on whether or not the boost key is pressed. You'll also probably want to use float
instead of int
not gonna get into why here but in this case float
is probably closer to how you were thinking it should work.
Currently you are moving the GameObject
before you update it's speed. This means that you're always using the previous frame's Speed value
var Speed : float = 0; // starts at 0 but it's ok because we set it before we use it
var BoostSpeed : float = 2;
var NormalSpeed : float = 1;
var CanBoost : boolean = true; // true or false can we boost rignt now?
var BoostAmount : float = 1; // (0 to 1) amount of boost left
var BoostDrainRate : float = 5; // seconds it should take for a full boost to drain
var BoostRechargeRate : float = 10; // seconds it should take to recharge fully from no boost
function Update () {
/*
* first decide which speed to use
*/
if (CanBoost && Input.GetKey ("z")) {
BoostAmount = Mathf.Max(0, BoostAmount - Time.deltaTime / BoostDrainRate);
if (BoostAmount == 0)
CanBoost = false; // we ran out of juice stop boosting
Speed = BoostSpeed;
} else {
BoostAmount = Mathf.Min(1, BoostAmount + Time.deltaTime / BoostRechargeRate);
if (BoostAmount == 1)
CanBoost = true; // we're full we can boost again!
Speed = NormalSpeed;
}
/*
* then move
*/
transform.Translate(transform.forward * Speed * Time.deltaTime);
}
i also noticed in your code you have multiple if statements for the same condition, you can combine them but in this case the statements you had inside the conditionals were redundant/unnecessary
You're right, there are some redundancies but I didn't bother to take them out. Your script works, but the problem is, it shoots me off diagonally backwards and to the right. And have no idea why. I realized even by making the variables negative, I would still move diagonally to the left. (The ship has no other scripts on it except for the mouselook.
Is your ship oriented correctly so that it's forward is it's positive Z axis? This script uses the Transform's local coordinate system's forward (`transform.forward`) as a direction. You could try switching to using the World coordinates system's Z axis ins$$anonymous$$d by switching transform.forward
for Vector3.forward
.
It's oriented in a weird way, yes. It needs to be that way. Well I tried Vector3.forward, and I realized that in all my other code, it uses Vector3.forward, so it works... only when I'm not boosting. Once I start boosting, that's when the problems start. Same thing happens only when I boost.
Yeah then the problem is the orientation of your object. A quick way to fix the orientation without making the object look different is to create a new GameObject
aligned correctly and then just place your original rotated object inside. Then put scripts on the new GameObject
.
In Unity positive x is always to the right, positive y is always up, and positive z is always forward. If you absolutely wanted to you could use an arbitrary direction as forward, however this means that every time you need to interact with that object and deal with rotation or direction then you need to take that rotation into account. In my experience it is never really worth it to go with a different direction as forward.
If you just cannot fix the ship's transform to point in the right direction then you will need to either calculate the actual forward or use something like Vector3.forward
which is the World forward (if your ship turns at all then this wont work)
Well I made the orientation of my object straight along the z-axis, but when the ship is facing in another direction, it won't boost. According to your last comment, if the ship turns it won't boost correctly. Can I fix this, or should I just you another script.
Your answer
Follow this Question
Related Questions
Boost Script Time problems 1 Answer
Reset speed after a specified time 0 Answers
Speed limit? 2 Answers
Pathfinding with specific end-direction and turning speed limit 0 Answers
Is it possible to have a gameobject in it's own timeframe? 0 Answers