How to fade text in Unity - c#

Heyo, I'm trying to make my GUI fade when not in use. Specifically, for a Minecraft-style tool bar, I want it to pop in when a player uses the scroll wheel and fade away in a few seconds after they're done scrolling. The GUI is working perfectly, but I can't get this fading thing to work. I assume once I get the selection text working, the sprites will follow easily, so I'll just talk about the selection text for now. The inventoryText starts the game at full white.
protected void OnGUI()
{
if (isInvGUIDirty)
{
// Update selection text
controllerInterface.inventoryText.CrossFadeAlpha(255f, 0, false);
controllerInterface.inventoryText.CrossFadeAlpha(0.1f, 2, false);
^ This results (when I move the scrollwheel) in the inventory text going full-white, and two seconds later shifting to a light grey. There is no sleek transition, and it is still clearly visible. This repeats every time I move the scrollwheel.
controllerInterface.inventoryText.CrossFadeAlpha(255f, 0, false);
controllerInterface.inventoryText.CrossFadeAlpha(0f, 2, false);
^ This results (when I move the scrollwheel) in the inventory text going full-white, and two seconds later shifting to invisible. This repeats every time I move the scrollwheel. Apparently 0.1 alpha makes all the difference o_O
controllerInterface.inventoryText.color = Color.white;
controllerInterface.inventoryText.CrossFadeAlpha(0.1f, 2, false);
^ This results in the inventory text slowly fading to a light grey, but no change on scrollwheel.
Any ideas on why CrossFadeAlpha() isn't working as expected?

Sorry, wasn't getting email updates from here :/. So what I ended up doing that worked was to write my own coroutine at the most basic level, which seemed to work...for some reason. Honestly, not sure why the previous didn't work. This operates on a CanvasGroup instead of the individual items too.
private void FadeInventoryGroup(float alpha, float delay)
{
if (fader != null)
StopCoroutine(fader);
fader = StartCoroutine(EFadeOutInventory(alpha, delay));
}
private IEnumerator EFadeOutInventory(float alpha, float delay)
{
yield return new WaitForSeconds(delay);
float progress = 1f - Mathf.Abs(inventoryGroup.alpha - alpha);
float start = inventoryGroup.alpha;
while (progress < 1f)
{
progress += Time.deltaTime;
inventoryGroup.alpha = Mathf.Lerp(start, alpha, progress);
yield return 0;
}
}

Related

How to move slider same amount with different max values when moving it by a percentage Unity

I have a slider that's max value changes based off the length of the currently selected audio clip. I am changing the slider's value when the user right clicks and moves their mouse left/right in a certain area. I want the slider to move faster/slower based on the distance the user has moved the mouse since the last frame. The problem is if my clip is say 135.35 seconds the slider moves extremely slowly. It only works well with a 1 second clip. I think I need to move it a different percentage based off of my movement but am having a lot of trouble getting the math right. I want the slider to move the same amount of distance for each distance value the user moves their mouse regardless of what the max value is. Any and all help is appreciated. Below is my current code but it is moving way too fast when the clip is 1 second and still a bit slow for a 135.35 second clip.
private void UpdateValue()
{
float DistanceMoved = FingerXPos - PrevFingerXPos;
float PercentageOfScreenMoved = Mathf.Abs(DistanceMoved) / Screen.width;
if (PercentageOfScreenMoved >= 0.005)
{
float percentangeChange = ((100 * PercentageOfScreenMoved) / SliderObj.maxValue);
if (DistanceMoved < 0)//slide to the left
{
SliderObj.value -= (percentangeChange*100);
}
else
{
SliderObj.value += (percentangeChange*100);
}
}
}
The Slider class has another getter/setter for .value, called .normalizedValue, and as the name suggests, it maps the range between min and max of your slider to 0-1.
So, you can use this:
float DistanceMoved = FingerXPos - PrevFingerXPos;
float PercentageOfScreenMoved = Mathf.Abs(DistanceMoved) / Screen.width;
if (PercentageOfScreenMoved >= 0.005)
{
SliderObj.normalizedValue += DistanceMoved/Screen.width;
}

Unity offset cycle always starts from beginning?

Alright, I am trying to start my animation clip from my animation controller in Unity in C# from a different position in the clip (an offset) every time it is triggered, but Im not understanding how cycle offset works (clearly) and my clip aways seems to start from the beginning regardless.
What I have is a walk cycle that is set off by a bool depending on if character is moving, but Im trying to have the character alternate (have it play the full walk cycle but through the increments of the animation that is played since it is played only when character is moving) their left and right steps. Otherwise it looks weird
How Ive tried to do this is have a float and increase that float ever time they move, and assign that float to the cycle offset of my animation. That way I figure the next time the animation is triggered it will start where it left off.
I am unsure if offset refers to time, frames, or percentage, so I've just done this so far:
if (animOffset >= 1f) { //was full clip
animOffset = 0.0f;
}
//anim help
currentPos = transform.position;
if (currentPos != lastPos) {
//print ("moving now");
animator.SetBool ("isWalking", true);
if (InputManager.stepCount % 2 != 0) {
print ("moving right");
animator.SetFloat ("walkOffset",animOffset);
} else {
print ("moving left");
animator.SetFloat ("walkOffset",animOffset);
}
animOffset += 0.1f;
} else {
animator.SetBool ("isWalking", false);
}
lastPos = currentPos;
Problem is Im not getting the desired effect and looking at the animation controller it seems the clip ALWAYS goes from the beginning, see:
The little blue bar always starts from beginning. What am I doing wrong here? How can I make an accurate run cycle offset?
You can link the "Cycle Offset" on the animation to a parameter on the Animator (in this case I've called Offset). You can then set this parameter in code:
Here's the animator window (ignore Speed, it's not needed here). I have one state OnState.
Here's the inspector window for the OnState state. I've ticked Parameter next to Cycle Offset and then it let me choose my parameter Offset.
In this code I get the animator and set the property Offset, which in turn can be set either programmaticly or through the inspector.
public class MyObject : MonoBehaviour
{
public float Offset= 0f;
Animator animator;
void Awake()
{
animator = GetComponent<Animator>();
animator.SetFloat("Offset", Offset);
}
Offset is a value between 0 and 1, so you would find where in your animation where your feed move and set that as a proportion of 0 and 1.
This might be what you are looking for AnimationState.normalizedTime to control the position of an animation.

Mouse drag direction tolerance

I'm working on a 2D game where the player can drag tiles around. It works in a way that the player clicks and hold a tile and depending in which direction the player moves the mouse from then on, the drag direction is decided.
The problem however is that this is overly sensitive. It might often be the case that the player starts dragging and wanted to drag vertically but due to the mouse sensitivity it turns out to drag horizontally (or vice versa).
Does anyone have an idea how to add a tolerance threshold to this dragging behavior? The relevant part in my code looks basically like this:
private void Update()
{
if (_isMouseDown && sourceTile != null)
{
_isDraggingTile = true;
/* Determine drag direction and drag target cell. */
Vector3 dragDistance = Input.mousePosition - _originMousePosition;
dragDistance.Normalize();
if (_dragDirection == DragDirection.None)
{
float f = Vector3.Dot(dragDistance, Vector3.up);
/* Mouse up drag. */
if (f >= 0.5f)
{
_dragDirection = DragDirection.Up;
_dragTargetCell = sourceTile.gridCell.upNeighbor;
}
/* Mouse down drag. */
else if (f <= -0.5f)
{
_dragDirection = DragDirection.Down;
_dragTargetCell = sourceTile.gridCell.downNeighbor;
}
else
{
/* Mouse right drag. */
f = Vector3.Dot(dragDistance, Vector3.right);
if (f >= 0.5f)
{
_dragDirection = DragDirection.Right;
_dragTargetCell = sourceTile.gridCell.rightNeighbor;
}
/* Mouse left drag. */
else if (f < -0.5f)
{
_dragDirection = DragDirection.Left;
_dragTargetCell = sourceTile.gridCell.leftNeighbor;
}
}
}
if (_dragTargetCell != null)
{
// Take care of moving the dragged tile!
}
}
}
Simply delaying the calculation of dragDistance by some frames doesn't turn out to work very well. I think what is needed is a solution to figure out the mouse movement and decide on which axes it moves farthest. Determining the drag direction as above will probably never work out well.
The problem with any collection of information is noise. In your case, the noise is defined by the wrong movement of the user. Nonetheless, it should be possible to minimize the effect of noise by averaging the values.
There are advanced algorithms used in DSP but I guess a basic averaging of the info should do in your case.
What you could try is that instead of moving in Update at once like you do, collect movement over several frames, then average all those frames and see if it goes better:
IEnumerator GetAverageMovement(Action<Vector3> result)
{
int frames = 0;
List<Vector3>list = new List<Vector3>();
while(frames < 30f) // half a second appr
{
list.Add(GetDirection());
frames++;
yield return null;
}
result(AverageAllValues());
}
GetDirection is just returning the delta between current and previous position, AverageAllValues simply adds all values in list and divides by list.Count (aka 30).
This should fix cases when the user move all the way right but a bit up at the end. The last bit should be canceled by the large right movement.
If that is still not enough, then you could add some logic within the method that if a value is too far gone from the average, discard it. I don't think you need this in there.
I think you should create a Queue of positions with limited size right after dragging .
by comparing final value of Queue and first value you can find the direction.
If you want to get better results you can get Variance of positions and get better results.

Make a basic running sprite effect

I'm building my very first game with XNA and i'm trying to get my sprite to run.
Everything is working fine for the first sprite.
E.g : if I go right(D) my sprite is looking right , if I go left(A) my sprite is looking left and if I don't touch anything my sprite is the default one.
Now what I want to do is if the sprite goes Right, i want to alternatively change sprites (left leg, right leg, left leg etc..) xCurrent is the current sprite drawn xRunRight is the first running Sprite and xRunRight1 is the one that have to exchange with xRunRight while running right.
This is what I have now :
protected override void Update(GameTime gameTime)
{
float timer = 0f;
float interval = 50f;
bool frame1 = false ;
bool frame2 = false;
bool running = false;
KeyboardState FaKeyboard = Keyboard.GetState();
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if ((FaKeyboard.IsKeyUp(Keys.A)) || (FaKeyboard.IsKeyUp(Keys.D)))
{
xCurrent = xDefault;
}
if (FaKeyboard.IsKeyDown(Keys.D))
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (timer > interval)
{
if (frame1)
{
xCurrent = xRunRight;
frame1 = false;
}
else
{
xCurrent = xRunRight1;
frame1 = true;
}
}
xPosition += xDeplacement;
}
Any ideas...? I've been stuck on this for a while..
Thanks in advance and let me know if you need any other part from the code.
You forgot to reset the timer, you could do this when you hit the timer interval condition. Also, 50ms seems a bit too small for an interval, maybe you could do like 400ms?
Other than that, looks good, it will do what you want.
Alternatively, you could look into making animated sprites for walking. What you do is make an image with the walking animation sprites one next to the other, of the same size. You draw only a portion of this image (one sprite) and move through them based on time.
Here is a quick code for what could be an animated texture:
class AnimatedTexture2D : Texture2D
{
int _numberOfImages;
int _currentImage = 0;
int _timeInterval;
int _spriteWidth;
public Rectangle DrawFromRectangle
{
get
{
return new Rectangle(_currentImage * _spriteWidth, 0, _spriteWidth, this.Height);
}
}
public AnimatedTexture2D(Texture2D entireImage, int spriteWidth, int numberOfImages, int timeInterval)
: base(entireImage.GraphicsDevice, entireImage.Width, entireImage.Height)
{
_numberOfImages = numberOfImages;
_timeInterval = timeInterval;
_spriteWidth = spriteWidth;
Color[] data = new Color[entireImage.Width * entireImage.Height];
entireImage.GetData<Color>(0, new Rectangle(0, 0, entireImage.Width, entireImage.Height), data, 0, entireImage.Width * entireImage.Height);
this.SetData<Color>(data);
}
public void Animate(GameTime gameTime)
{
int totalImageTime = _timeInterval * _numberOfImages;
int currentPoint = (int)gameTime.TotalGameTime.TotalMilliseconds % totalImageTime;
_currentImage = currentPoint / _timeInterval;
}
}
Usage is fairly simple:
1) declare it somewhere:
AnimatedTexture2D animatedTexture;
2) initiate it with your texture (i had a 2560x64 sequence of 40 64*64 images), where individual images are placed next to each other horizontally:
animatedTexture = new AnimatedTexture2D(Content.Load<Texture2D>(#"Textures\Loading"), 64, 40, 20);
3) in your Update() method, call:
animatedTexture.Animate(gameTime);
4) in your Draw() method, call:
SpriteBatch.Draw(animatedTexture, new Rectangle(20, 20, 64, 64), animatedTexture.DrawFromRectangle, Color.White);
Don't forget the DrawFromRectangle in part 4! (notice that the destination rectangle uses the declared individual part width, not the entire texture width which is in my test 2560 pixels)
Now, in your code you could forget the interval part, and the gametime part, and you could just use this instead of the default one!
Also, if you don't like my timing code (its ultra simple but lacks a way to reset the animation) change it so you have an elapsed time variable, and add to it like you do in your own code, and use that to change _currentImage. You could even make that variable public so you can use it to reset the animation (or set it to a specified point).
Of course, you could also make the default one an animated texture with only one frame so you can use the same code everywhere. Hope this helps!
You need to keep the last time Update(..) was called, and the interval should be... well.. an interval, that is, the difference between ElapsedGameTime and last call to update ElapsedGameTime.
Do it with a new member of your class (LastElapsedGameTimeUpdateCalled) or a static member of your sub.
If you know that every animation is going to have the same number of frames you could keep 3 variables per sprite (encapsulate in class for best results).
BaseFrame is an integer to hold a global animation number.
SubFrame is an offset in to the animation that holds which frame you are currently on.
FrameAccumulator to hold timing information.
Each time update is called, add the number of ms since the last update to the accumulator. Once the accumulator goes above your animation timing, increment SubFrame, and reset the accumulator. Check to see if subFrame is greater than the number of frames for each animation, and if it is, set it back to 0. You can get the real frame index from this by adding BaseFrame + Subframe. When you need to display a different animation just change the BaseFrame.
Lets say that each animation has 3 frames, and you have 2 total animations. You would have 6 total frames. RunLeft would be BaseFrame 0, and RunRight would be BaseFrame 3. That should easily give you the frame number to draw.

Xna adding gravity to a 2d sprite

I am trying to simulate gravity in my first xna 2d game. I have the following
//Used for Jumping
double elapsedAirTime = 0.0;
double maxAirTime = 8.35;
//End Jumping
So I am trying to move the sprite up by a certain amount while the elapsedAirTime < maxAirTime
However, there is some issue where my code only seems to move the sprite up once and not multiple times during this segment of time. here is the code in my "player.cs" class, or the update method of the class.
if (newState.IsKeyDown(Keys.Space))
{
if(oldState.IsKeyUp(Keys.Space))
{
//if we are standing or jumping then change the velocity
if (playerState == PlayerStates.Standing)
{
playerState = PlayerStates.Jumping;
this.position.Y -= (float)(30.0 + ((1.2)*elapsedAirTime)*elapsedAirTime);
}
}
}
//if we are jumping give it some time
if (playerState == PlayerStates.Jumping)
{
if ((elapsedAirTime < maxAirTime) && position.Y < 3)
{
this.position.Y -= (float)(30.0 + ((1.2) * elapsedAirTime)*elapsedAirTime);
elapsedAirTime += gameTime.ElapsedGameTime.TotalSeconds;
}
//otherwise time to fall
else
{
playerState = PlayerStates.Falling;
}
}
//add gravity to falling objects
if (playerState == PlayerStates.Falling || playerState == PlayerStates.Standing)
{
//if we are above the ground
if (this.position.Y < windowBot - 110)
{
//chnage state to falling
playerState = PlayerStates.Falling;
this.position.Y += 3.0f + ((float)(gameTime.ElapsedGameTime.TotalSeconds));
}
else
{
playerState = PlayerStates.Standing;
elapsedAirTime = 0.0f;
}
}
Any help is much appreciated, please and thank you!
To give your sprite the feel of gravity, you should add velocity and acceleration to your Sprite class. Then, create an Update method for the Sprite, and have acceleration be added to your velocity every update, and velocity added to position every update. Position should not be based on the amount of elapsed air time. You can set the acceleration to a constant gravitational value, and then add to the velocity of the Sprite whenever you jump. This will create a flowing parabolic jump that looks nice. If you want to include timing, you can pass the GameTime into the Sprite's Update method, and use it as a modifier on the velocity. Here is an example Update method:
void Update(GameTime gt)
{
int updateTime = gt.ElapsedGameTime.TotalMilliseconds - oldgt.ElapsedGameTime.TotalMilliseconds;
float timeScalar = updateTime / AVG_FRAME_TIME;
this.velocity += this.acceleration * timeScalar;
this.position += this.velocity;
oldgt = gt;
}
If you use timing, this method is a little complicated. You have to keep track of how much time the update took, then divide it by the average amount of time an update or frame should take to get the amount you should adjust your velocity by. Without timing, the method is very simple:
void Update()
{
this.velocity += this.acceleration;
this.position += this.velocity;
}
I would suggest using the simpler method until you understand exactly how timing works and why you need to implement it.
It looks like this line is at fault:
this.position.Y -= (float)(30.0 + ((1.2) * elapsedAirTime)*elapsedAirTime);
I think you will find that this updates the sprites position quicker than you imagine, the sprite will move 330 pixels up the screen in 10 updates (assuming Game.IsFixedTimeStep == true) that is 1 tenth of a second realtime
It is likely that this is just updating so quickly that you don't get a change to see it rise before the && position.Y < 3 condition kicks in and changes the playerState.
It looks like you are trying to say - jump at a rate of x pixels per second for upto 8.5 seconds so long as space is held.
What you need for that is to change the calculation to this.position.y -= (float) (30 * gameTime.ElapsedGameTime.TotalSeconds), this will give a very liner movement to the jump action but it will mean that the sprite jumps at exactly 30 pixels per second.
If Game.IsFixedTimeStep == true - which is the default - the update gets called 60 times per second so gameTime.ElapsedGameTime.TotalSeconds is going to be about 0.1 every update. If something happens to cause an update to skip (rendering issues for example) then update will get delayed and gameTime.ElapsedGameTime.TotalSeconds may be 0.3 (the 2 updates skipped) but the formular still works out the correct jump rate.

Categories

Resources