Hi everyone.
I’m trying to do an power bar that lets player to jump higher longer he keeps space pressed down. I can see the powerbar growing when I hold space down, this is where the first problem lies. It can go over the full width i’ve set in my code.
Then we can continue to second problem, no matter how long I hold space down, player won’t jump higher.
Here’s the code so far. Sorry for not commenting it at all.
using UnityEngine;
using System.Collections;
public class JumpBar : MonoBehaviour {
public float fullWidth = 256;
private float thePower;
public bool increasing = false;
public bool jumping = false;
public float barSpeed = 25;
private GameObject player;
// Use this for initialization
void Start () {
player = GameObject.Find("Player");
// Set the jumping power bar at zero at start
Rect pos = guiTexture.pixelInset;
pos.xMax = guiTexture.pixelInset.xMin + barSpeed * thePower;
guiTexture.pixelInset = pos;
}
// Update is called once per frame
void Update () {
if(!jumping && Input.GetButtonDown("Jump")) {
increasing = true;
}
if(!jumping && Input.GetButtonUp ("Jump")) {
increasing = false;
player.rigidbody.AddForce(Vector3.up * thePower);
}
if(increasing) {
thePower += Time.deltaTime * barSpeed;
thePower = Mathf.Clamp (thePower, 0, fullWidth);
Rect pos = guiTexture.pixelInset;
pos.xMax = guiTexture.pixelInset.xMin + barSpeed * thePower;
guiTexture.pixelInset = pos;
}
if(increasing == false) {
thePower = 0;
Rect pos = guiTexture.pixelInset;
pos.xMax = guiTexture.pixelInset.xMin - barSpeed * thePower;
guiTexture.pixelInset = pos;
}
}
}
So if someone could tell me where I’ve gone wrong? I think this is a really simple problem, but I haven’t been sleeping for two nights so my brains won’t really work at all right now and can’t seem to find where I’ve gone wrong.