-1
\$\begingroup\$

I have created a gameobject(cube) and written code to move,mean while the processbar get loading.when the processbar is loaded fully then the object should stop moving.Can anybody please help me coding out.

code used for doing progress bar

var progress : float = 100;
var pos : Vector2 = new Vector2(20,40);
var size : Vector2 = new Vector2(60,20);
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;

function OnGUI()
{
    GUI.DrawTexture(Rect(pos.x, pos.y, size.x, size.y), progressBarEmpty);
    GUI.DrawTexture(Rect(pos.x, pos.y, size.x * Mathf.Clamp01(progress), size.y), progressBarFull);
}

function Update()
{
    progress = Time.time * 0.05;
}

function OnMouseEnter()
{
    progressBarFull=progressBarEmpty ;
}

enter image description here

Suppose If I place a button,when I click the button the bar should get loading.For that I have written the code

progress = Time.time * 0.05;

to if(button)

but the progress bar is not getting loaded.What will be the problem

code changed is

#pragma strict

var progress : float = 100;
var pos : Vector2 = new Vector2(20,40);
var size : Vector2 = new Vector2(60,20);
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;

function OnGUI()
{
GUI.DrawTexture(Rect(pos.x, pos.y, size.x, size.y), progressBarEmpty);
GUI.DrawTexture(Rect(pos.x, pos.y, size.x * Mathf.Clamp01(progress), size.y), progressBarFull);
if(GUI.Button(new Rect(300,300,60,30),"Start "))
{
progress = Time.time * 0.05;    
}
}

    function OnMouseEnter()
    {
      progressBarEmpty=progressBarFull ;
    }

This code is working like,the progress bar is getting incremented on each click of the button.I need to display it at a single click.What will be the solution,can anybody help me out
\$\endgroup\$
12
  • \$\begingroup\$ Would you mind to elaborate your question a little bit more? Maybe include some code snippet? Right now it seems too broad to provide any useful feedback. \$\endgroup\$
    – angarg12
    Aug 7 '14 at 7:37
  • \$\begingroup\$ green color is the progress bar and the box is the object,I progress bar keeps on increasing according to that the object keeps moving,when the progress bar is full(fully green) then the object should stop moving \$\endgroup\$ Aug 7 '14 at 8:32
  • \$\begingroup\$ Please post some code. \$\endgroup\$
    – jparimaa
    Aug 7 '14 at 8:38
  • 4
    \$\begingroup\$ This question appears to be off-topic because it is about writing code for you. \$\endgroup\$
    – bummzack
    Aug 7 '14 at 9:41
  • 1
    \$\begingroup\$ why on earth would you want do do that with a progress bar? \$\endgroup\$ Aug 7 '14 at 9:52
2
\$\begingroup\$

Well I'd like to comment on the question but I don't have 50 rep. Anyways: If we knew why you were doing this (specifically) then we might be able to help.

To know when to stop moving the object, you'll need a condition as such:

int maxProgress = 100; //the value at which the progress is full
if (progress > maxProgress) //if it is full
{
     var cube = GameObject.Find("cube"); //find the cube on the scene (or whatever you have named it in the heirarchy
     cube.transform.velocity = Vector3.zero; // set the velocity to 0
}

I hope this basic code can get you started using conditions in your code to check for happenings in your game.

(PS. If you want the cube's position to match the bar position, I'll leave that to you to figure out. Hint: it involves math, using the following pieces of information: the bars position and size, and the progress and maxProgress. Think percentages, and set the cube's position with your result.)

\$\endgroup\$
2
  • \$\begingroup\$ You stumbled over something I did as well. The progress-bar is definitly full after 20 seconds (20*0.05 = 1). As he clamps the value to 1 in OnGUI(), I assume that everything above 20 is considered beeing greater than maxProgress (which should be 20). Also you should not check progress > maxProgress but progress >= maxProgress, otherwise you stop too late. \$\endgroup\$
    – JFBM
    Aug 7 '14 at 12:02
  • \$\begingroup\$ You're right, I'll be one frame off. But I was also nervous that he was initializing progress to 100 at the top (I think that should be max progress), which would've caused the condition to hit the first frame if not places below the Time line. Easily fixable, after which the >= is better. \$\endgroup\$
    – Ideae
    Aug 7 '14 at 12:10
0
\$\begingroup\$

There are several possibilities.

If your cube is user-controlled, it probably has something along the line of

if(input.getKeyDown(...))
{
    //move
}

written either in your Update or your FixedUpdate.

Simply test the progress and the userinput like this:

if(!ProgressBar.isFull() && input.getKeyDown(...))
{
    //move
}

This also works with a little bit of work for automated movement.

You can also deactivate the Movementscript from the ProgressBar-Script or any other script monitoring the ProgressBar-Script.


Bonus Code Review:
I seriously have no real idea what your doing in your OnMouseEnter().
It looks like you set your "BarFull"-Texture to the "BarEmpty"-Texture, which would result in the progressBar never again filling up visually, simply because the "BarFull"-Texture is gone. I can only assume that this is not what you want.

You will also have some kind of flickereffect at the beginning, because your progress is 100 at the beginning, but gets set to 0 (or maybe 0.05) in the first update. Depending of the execution order of Update() and OnGUI(), this can cause problems.

You use Clamp01() in OnGUI() to keep the value from getting to big, but your progress will get too big. If you pull the Clamp01() in the Update()-Function, it is better visable what you expect from the variable, allowing better understanding (also from your side) in the future.

You also have a definite time your ProgressBar will need to fill up, which is 20 Seconds. This can be of course the intended effect, but I would think a ProgressBar measures the progress of something like Fileloading, Items gathered or such. Maybe consider renaming it to TimeBar.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .