React Native: Continue animation with last velocity of touch gesture - animation

In my React Native application I have some tiles (wrapped in a View for the example) which are half of the full width wide. They act as buttons and slide to the opposite side to open a menu. When I perform a swipe gesture and release the finger before the slide reaches its final position, I want the slide to animate to its final 'opened' position. The animation should start with the last velocity of the touch gesture for a smooth impression.
I implemented different variations but did not find a good solution (You can find my test-component in my GitHub repository). My View has a PanResponder to manage the gesture and to get the velocity. I started to use the Animated library, but the provided methods do not solve my problem. The only method where I can pass a initial velocity for the animation is the decay, but I can't pass a parameter where the animation should stop. With a timing animation I can set a final value, but can not pass a initial velocity (so the animation starts with a velocity of 0 which looks very jumpy). I tried to combine these two methods, but that does not work properly.
On iOS I could use a horizontal ScrollView with pagingEnabled, which shows the desired effect - but then I do not have the feature on Android.
Any ideas how I can solve this problem and show a smooth animation, starting with an initial velocity and ending on a given position, after the touch gestures end?
Thanks for your help!
EDIT I added the link to my last test component...

You can get a close approximation of the velocity by setting the duration of the timing animation
const duration = Math.abs((this.props.MAXDISTANCE - Math.abs(distanceMoved)) / velocity);
MAXDISTANCE is your final position
distanceMoved is the current position (gestureState.dx)
velocity is the current velocity (gestureState.vx)

You can use Animated.decay or Animated.spring to achieve this effect.

Related

Unity3d Updating position with animation simultaneously

I have an animation for humanoid models to simulate climbing. I have been looking for a way to stimulate this animation when the model comes next to the window. I used the triggers to determine where the model is and it worked. However, when I execute the animation, the position of the model is not being updated according to the animation. I am using offmesh links and nav mesh agent and I disable nav mesh agent when the model triggers. How can I use the animation and provide the update simultaneously?
Animation Properties
Thanks in advance.
I don't think the animation should take care of the movement. You should control that somewhere else. You could make the animation climb from y = 0 to 5 but then it won't work if your ladder is at y = 3.
You'd better have a method that is called from the animation using AnimationEvent so that when you trigger the animation, it also triggers a movement of the object upwards/downwards regardless of the current position of the object. It would simply use Translate and disabled input until the animation ends.

Unity - How To Animate UI Text Smoothly?

I just wanted to know how do you transition a UnityEngine.UI from one position to another smoothly?
My current code is this:
GameObject rank1;
GameObject rankSlot1;
rank1 = GameObject.Find("Rank1");
rankSlot1 = GameObject.Find("RankSlot1");
rank1.transform.position = new Vector3(Mathf.Lerp (rank1.transform.position.x, rankSlot1.transform.position.x, 0.1f), rankSlot1.transform.position.y, 0);
But it seems like the Mathf.Lerp doesn't work :/
Thanks!
EDIT: All these gameobjects do have Rect Transform as they are a children of Canvas
It depends where do you use Lerp ? Are you calling it once ? or in Update(). Calling in update with correct parameters should work.
Beside Lerp, you can use Animator component with position curves to move from one point to another.
There is another option to use LeanTween plugin, it is a free plugin on asset store.
LeanTween.move(gameObject, yourFinalPosition, duration);
If using an animator you get access to the animation curves so you can get really nice controlled animations
Tricky part is if you are wanting to use 'apply root motion' (have the animations start where they are instead of fixed positions) you need to apply it to something that is considered ROOT:
- create an empty gameobject - with a normal transform
- parent the canvas with text to this new gameobject
- apply the animations to the gameobject transform ( not the UI rectTransform)
- on the animation in the inspector you should now be able to click on the 'generate root motion curves' and also select the 'apply root motion' check box for the Animator
hope this helps,, took a few hours to get working :) I used this to float up enemy damage text displays

wxToolBar changing device coordinates

Using the mouse I am drawing 2D shapes on the client area of a MDIChildFrame. Recently I have added a wxToolBar to the frame and when I now draw a shape on the client area it seems that the points have shifted by the size the toolbar. Imagine that with mouse I am clicking on (100,100) and drawing a line to (150,150); however, the line appears somewhere (75,75) to (125,125). By the way, wxMouseEvent GetPosition(); reports (100,100) to me.
Removing the toolbar fixes the problem however, I want to keep the toolbar for ease of tool selection.
I use the code:
m_ToolBar=new wxToolBar(this, wxID_ANY);
m_ToolBar->AddTool() //
m_ToolBar->Realize();
this->SetToolBar(m_ToolBar);
Any ideas will be appreciated.
You can always use wxWindow::GetClientAreaOrigin() to manually offset the coordinates by the toolbar height but normally this shouldn't be necessary, and if this doesn't happen with a "normal" frame but only happens with wxMDIChildFrame it would be a bug in wxWidgets that should be reported as usual.
It's also recommended to not draw over wxFrame itself but rather put a wxWindow into it and draw on it. This should also take care of your problem.

Unity 4.6 - How to scale GUI elements to the right size for every resolution

The new Unity 4.6 comes with a new GUI, when I change de resolution on Unity the UI Button scales perfectly but when I test on the Nexus 7 device the Button looks too small. Any idea how to solve this?
Unity's new GUI system uses "anchors" to control how gui elements (like buttons) scale in relation to their parent container.
Unity has a tutorial video on how to use the new "Rect Transform" component (where the anchors are configured) here: http://unity3d.com/learn/tutorials/modules/beginner/ui/rect-transform.
The last half of the tutorial is all about anchors. That page has links to the entire tutorial series. It's not too long. You should watch the whole thing.
Specific to your question:
The anchors are visible in your first screen shot. They are those 4 little arrows at the top left of your button.
Right now, your button is only anchored by it's top left corner.
The two right anchors need to be dragged to the right so that the right edge of your button is anchored to a space inside its parent container.
Depending on your situation, the two bottom arrows may need to be dragged down so that the bottom edge of your button is anchored as well.
The video I linked above covers all this in detail.
Lastly, for the font size to scale nicely on different resolutions, you will need to add and configure a reference resolution component to the base canvas of your UI, as Ash-Bash32 wrote earlier.
Update: The best way to add a Reference Resolution component is through the inspector window for the base canvas in your UI.
1) click the "Add Component Button" at the bottom of the inspector.
2) type the word "Reference" in the search filter field.
3) select the "Reference Resolution" component in the search results.
The Reference Resolution is now renamed as Canvas Scaler.. Along with the renaming they have added many more features for the dynamicity of the Canvas. You can go through the Unity Doc of Canvas Scaler and also take a look at this article for a practical example of how and why to use Canvas Scaler. Also make sure you use the Anchor Points to good effect to make this more robust...
To Scale UI added the ReferenceResolution Component to the Canvas you want to scale.
P.S. Theres no Documention for ReferenceResolution
If you want the button to be the same size for all screens and resolutions, you have to add the canvas scaler component to the canvas and the set the screen match mode to: match width or height, here is the link to the docs, this helps a lot if you want to aim to different sizes or resolutions:
http://docs.unity3d.com/Manual/HOWTO-UIMultiResolution.html
This becomes giant and convoluted once you start laying things out in code AND using a canvas scaler, so I wish to provide a thorough answer to save someone the hours I went through.
First, don't use anchoredPosition to position anything, unless you fully realize it is a 0.0 to 1.0 number. Use the RectTransform localPosition to do the actual laying out, and remember it's in relation to the parent anchor. (I had to lay out a grid from the center)
Second, put a canvas scaler on the parent layout object AND the inner ui pieces. One makes the layout in the right position, the other will resize your elements so they actually show up right. You can't rely on the the parent unless the children also have scalers (and Graphic Raycasters to touch them).
Third, if you have a scaler, DON'T use Screen.width and height, instead assume the screen is the same value you put for the scalers (hopefully you used the same, or know what you're doing). The screen width always returns the actual device pixels, retina devices too, but the canvas scalers DO NOT account for this. This probably gives unity the one remaining way to find actual screen dpi if your game wants it. Edit: This paragraph applies to any parent canvas connected to the code doing your laying out. Not stray canvases, you can probably mix it up. Just remember unity's guidelines on performance with canvases.
Fourth, the canvas is still a bit buggy. Even with the above working, some things don't render until you delete and recreate a canvas, if you re-open the scene or it crashes. Otherwise, the above is the general "rules" I've found.
To center a "grid of things" you can't just use half of the canvas scaler's width or height, you have to calculate the height of your grid and set the offset by half of it, otherwise it will always be slightly off. I just added this as an extra tip. This calculation works for all orientations.

Unity 3d show specific frame of animation

I'm trying new unity 3d options for 2d games. I'm trying to create background that changes depending on actions i made. So if i press button one i get sprite one as background and if two i get sprite two. Since I have 32 options, I figure out the best way would be to have an Animator, that changes frame depending on button click. So i created animator and animation. But the problem is I can't set time to where to stop animation to show selected frame.
I'm trying like that:
Animator ani=background.GetComponent<Animator>();
ani.animation["field_back_anim"].time=0.5f;
ani.speed=0;
But it fails at second line with that error:
MissingComponentException: There is no 'Animation' attached to the "background" game object, but a script is trying to access it.
You probably need to add a Animation to the game object "background". Or your script needs to check if the component is attached before using it.
However if I do no code the animation just plays trough all 16 frames. So i gues there is animation after all.
On the background GameObject I have 2 components first is sprite renderer and second is animator. When I open animator in Animator view i see there green rectangle saying Any state and yellow one with "field_back_anim". I don't get what i'm doing wrong.
I will also except any other solution that does the following.
Thanks!
The animator component is used to control transition between many animation clips. If you're going to playing an animation clip on a gameobject, an animation component is proper, not the animator. Remove the animator and add an animation component to your background gameobject in the inspector. If you set animation property to field_back_anim, your gameobject will animate well. Manipulation codes should be changed like below.
Animation ani = background.GetComponent<Animation>();
ani["field_back_anim"].time = 0.5f;
ani["field_back_anim"].speed = 0;

Resources