I have a simple sprite sheet that I'm using in a Unity project:
It has frames for walking and standing in each of four directions. I've cut the image up using Unity's Sprite Editor and made a separate Animation object for each series, all of which fit neatly into a single animation controller:
I've added the animation controller to an Animator component on a GameObject in my scene. This is where I start running into trouble. I can't for the life of me figure out how to change between the animations in my code.
You are not supposed to create the connections between your animations in a script. What you do is you create the connections in the Animator and set up variables for if you are standing or walking and in which direction you are going. Then you just edit these variables in a script and the Animator takes care of chosing the right animation.
This video shows it in action: Example
(you see the connections and in the bottom left the variables he set up)
Quick google of "mechanim 2d tutorial" gives you: Tutorial
edit:
Too long for a comment :)
Basically what i would do is create 2 variables, one bool named "Standing" and one Integer named "Direction". Then you make one connection from AnyState to every other state you have(Rightclick on AnyState to make the transitions). Maybe order them in a circle around AnyState.
After this, click on the connections and in the Inspector set the conditions by chosing "Standing", then add one more and set it to "Direction" "Equals" and the number for the direction.
Then in your script somewhere in your Update function you might have something like:
private readonly int UP = 0;
...
if(speed.magnitude > 0.0f)
Animator.SetBool("Standing", false);
else
Animator.SetBool("Standing", true);
if( Condition for walking up? )
Animator.SetInteger("Direction", UP);
if( Condition for walking down? )
Animator.SetInteger("Direction", DOWN);
...
this might not work directly, but you get the idea. You might want to use a switch for the direction depending on how you set it up.
Why even use code? Just hook it all up in the Animator using parameters and just change the parameters as needed.
The Animator.CrossFade function will achieve the desired results:
C#:
Animator animator = this.GetComponent<Animator>();
animator.CrossFade("WizzyWalkDown", 0);
It may seem a little excessive, or counter-intuitive, but remember that the AnimationController class was built to make transitions between animations easy. It can still very much be used for simpler animation types like this, though!
Related
I'm fairly new to Unity and not quite sure how to handle this problem.
I have two images, one has clouds on it (day) and one has stars on it (night). What I want to do is show the clouds in the top of my scene and the stars on the bottom. There is a ground object in the middle of the screen where the player will be walking on, this should be the dividing line between the two images. The ground however is not one straight line but can have height differences.
The "solution" I came up with is to use the ground object(s) to slice the images so it kinda serves as a dividing line. But not sure if this is even possible. Maybe I could do something with 2 different camera's or mask the images somehow.. (Just throwing my own thoughts in here as well) I'll be fumbling around with these things in between and try to keep the topic up to date with what I tried.
I put in an attachment to (hopefully) make it more clear.
Greets,
Lukie
attachment: https://imgur.com/a/lblJXPi
The first solution to my mind was preparing a tileset. If you're not going to design a different section every time. So if you're not going to do a computer design. You can do it yourself by adjusting the size.
You can also dynamically generate the stars with the -y axis of the ground object and the clouds with the + y-axis. You can use instantiate function
Example:
public GameObject clouds;
public GameObject stars;
// Start is called before the first frame update
private void Awake()
{
Instantiate(clouds, new Vector3(this.transform.position.x, this.transform.position.y + 3.625f, this.transform.position.z), Quaternion.identity);
Instantiate(stars, new Vector3(this.transform.position.x, this.transform.position.y - 3.625f, this.transform.position.z), Quaternion.identity);
}
Of course, the background design that you will use here must be sustainable.
Dynamic Background
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
I am new to designing games and have been having trouble for the past couple of days with the animator. I have downloaded a free asset off the Unity store that included a free sprite character and also its animations for idle, walk, run, and jump. My issue is when I go to use this asset pack in my game, I can not for the life of me figure out how to transition between idle->walk, and any state->jump. I believe it has to do with no parameters being set up, as I want the walk animation to occur when my character is moving. The issue is that it seems to be stuck in idle mode. In the animator the transitions are set up, but I can not figure out how to let the animator know when I am moving, and when I am actually idle. How can I connect my characters movement scripts to be used in conjunction with the animation? Do I need to write a new script, that uses new parameters I make in the animator, or am I completely missing something? Again I am very new at this, so I apologize if this is a dumb question but I cannot figure it out. All the videos I have watched only show how to build the animations and put them into the animator, and nothing about the scripts or parameters in specific. Thank you!
Look at the animation transition requirements, then set the required values. For example:
Animator anim = obj.GetComponent<Animator>();
anim.SetTrigger("running");
or
anim.SetFloat("speed", 2);
or
anim.SetBool("running", true);
One small thing that could be going wrong: If you somehow have a reference to the prefab instead of the instantiated object in your scene, it will not work. And to be sure it's not working, play the game, find the object with the Animator in the Hierarchy, click it, and open the Animator window. You can see which state is active.
If this doesn't help, can you describe the transitions in more detail?
This appears to be such a common problem, that finding answer to my specific case is near impossible, because of the amount of noise in google or forum searches.
I have imported object to unity from blender. This objects had smaller objects inside it. Those smaller objects have animation on them, which was created in unity. If I open the animation window (Window->Animation) and press play - I can see correct movement of an object. Animation is set to play automatically in inspector, culling type is always animate. If I play the game however, animation does not play. Why?
add the animation to the main object, then animate the smaller object...
you have to put animation component to main object, then animate the small part...
it is important...
it should work
I Could not find any unity inspector based answer. For now, the best option I have is to create new script, and in update() check if the animation is playing. If not, play it. Very inefficient, so to whoever might stumble upon this post in the future, I suggest you try to add this.animation.play() in setting up part of the script.
I know it's kinda late, but this is for others having this same issue.
Animations in Unity have types (2 of which I know).
Type 1 can be attached to an object using the Animation Component.
Type 2 can only be used in Animators (Mechanim).
Animations imported with FBX files are type 2 (and as far as I know, animation type in FBX files can NOT be changed), so they will usually not work when attached directly to an object with "Animation". They should be used with Animators and Animator Controllers. Just create a New Controller and add the animation. It will become the default animation state and play automatically.
If you want it to loop, click on the FBX asset, and in the Animation tab, choose Loop Time and click Apply.
I am working on a program, that uses THREE.RollControls, when the user goes too far away from the center of the screen, they tend to get lost, so I am working on creating a function that reorients them, facing the center of the scene.
What I had intened to do was simply call the following:
camera.lookAt(scene.position)
However, this has no affect. From what I was reading on different stack overflow questions specifically this:
ThreeJS camera.lookAt() has no effect, is there something I'm doing wrong?
It seems like their solution was to do the camera position change using the controls, rather then changing the camera itself.
I do not believe there is any 'Target' in the Roll Controls, so I don't know how I can reset where the camera is looking at based on a THREE.Vector3() Is there a simple way to do this, or will I basically have to:
So far I have 'attempted' to do the follow:
- Calculate the difference of position of the camera with the position of the scene.
- Normalize this vector
- Subtract it from the direction forward of the camera
- use this vector in controls.forward.add(thisVector)
but this doesn't do at all what I want (probably because I have no idea what I'm doing)
Thank you in advance for your time!
Isaac
The same thing bugged me too about the RollControls but I took a different approach in solving the problem. Since the controls are in the example code (in r55) you can modify the controls, as they are not part of the core library. You can see my modifications at http://www.virtuality.gr/AGG/EaZD-WebGL/js/three.js/examples/js/controls/RollControls.js
I introduced a local variable called mouseLook because I could not use the this.mouseLook. I initialized it to false and I only make it true when there is a button press i.e. when navigating in the scene. That solved my problem.