- Home /
How do make player balloon inflate and float upwards?
So I'm working on a game rn and I'm currently stuck on how I can manage to implement this. I'll to try explain at my best with some picture to easily understand.
Basically I want it every time that you hold down the spacebar, it makes the balloon inflate and it float upwards.
I also want it that the more you hold it down, the more bigger it gets. and the more bigger it gets, the more higher it gets and stays up for. After some time maybe around 10 seconds, I want it to shrink back to its original size and it falls back down.
So far I haven't been able to find anything that's helpful for what I want to do. Is anyone able to tell me if there is a way to implement this? and if so, how can I do it?
What part are you struggling with? The input, the balloon size, the floating or the timer?
The input I think can do, but rest is what I'm struggling with.
Alright. This mechanic seems mostly timer based. So you can update a variable over time, when holding the spacebar. This variable can then be used to deter$$anonymous$$e the size of the balloon and the height of the balloon. Something like:
public float timer = 0f;
public float heightRatio = 1f;
public float scaleRatio = 1f;
public void Update()\
{
//Increase timer when space is held, decrease when released.
if(Input.GetKey(Keycode.Space))
{
timer += Time.deltaTime;
}
else
{
if(timer > 0)
{
timer -= Time.deltaTime;
}
else
{
timer = 0;
}
}
//Set the height of the gameobject proportionate to the timers value.
//If you want to rise 2 units every seconds, you set the ratio to 2.
transform.position.y = timer * heightRatio;
//Set the scale of the gameobject proportionate to the timers value.
transform.localScale = Vector3.One * (scaleRatio * (timer + 1f));
}
Answer by Farmerfromtexas · Dec 17, 2021 at 02:32 PM
Hey hey, try this script on your balloon game object!
It should be able to achieve everything you described and you can tweak values in the inspector to get the desired effect. Hope it helps! :)
private Rigidbody rb;
public float maxSize = 3;
[Range(0, 1)] public float riseSize = .5f; // Determines at what percentage of the max size the balloon Gameobject will start to rise.
public float timeToReachMaxSize = 1;
public float timeBeforeDeflatingInSecs = 1;
private float timeInflating;
private float startDrag; //equals the 'Drag' setting of the Rigidbody component.
public float maxDrag = 10; //has to be higher than the 'Drag' setting of the Rigidbody component.
private float speed;
public float maxSpeed;
private Vector3 baseScale;
private Vector3 targetScale;
private Vector3 sizeToRise; //the scale factor at which the balloon game object should start rising
private bool inflating = false;
private float timer;
void Start()
{
rb = GetComponent<Rigidbody>();
baseScale = transform.localScale;
targetScale = baseScale * maxSize;
sizeToRise = baseScale * (riseSize * maxSize);
startDrag = rb.drag;
}
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
timeInflating += Time.deltaTime;
inflating = true;
timer = 0;
}
else
{
if (inflating == true)
{
timer += Time.deltaTime;
if (timer > timeBeforeDeflatingInSecs)
{
inflating = false;
}
}
}
if (transform.localScale.magnitude >= sizeToRise.magnitude)
{
rb.useGravity = false;
rb.velocity = new Vector3(0, speed, 0);
}
else
{
rb.useGravity = true;
}
if (!inflating)
{
timeInflating -= Time.deltaTime;
}
timeInflating = Mathf.Clamp(timeInflating, 0, timeToReachMaxSize);
transform.localScale = Vector3.Lerp(baseScale, targetScale, timeInflating / timeToReachMaxSize); // Inflate/Deflate over time.
rb.drag = Mathf.Lerp(startDrag, maxDrag, InverseLerp(baseScale, sizeToRise, transform.localScale)); //Determines the drag of the balloon. The bigger/the more inflated the balloon the faster it will rise and vice versa.
speed = Mathf.Lerp(0, maxSpeed, InverseLerp(sizeToRise, targetScale, transform.localScale)); //Determines the upwards velocity depending on the size of the balloon game object.
}
public static float InverseLerp(Vector3 a, Vector3 b, Vector3 value)
{
Vector3 AB = b - a;
Vector3 AV = value - a;
return Vector3.Dot(AV, AB) / Vector3.Dot(AB, AB);
}
this is great! although is there a way we can make it automatically deflate with a cooldown system?
In the inspector there is the "Time Before Deflating In Secs" variable of the script. The balloon will wait that amount of time before it starts deflating from the moment you let go of the space bar. You could set the desired cooldown as its value. Is that what you have in $$anonymous$$d?
if it is than yeah, thats what i have in $$anonymous$$d