Get Current Animation Frame or Sprite Name - unity3d

In Unity 2D, is there a way to get the current sprite used either by a SpriteRenderer or an Animation? The idea I have is to have multiple GameObjects to use for a player character, and from the main one which receives animations and the script, edit the others based on the sprite currently used.
Now, I know that to get the current sprite name I can do this:
GetComponent<SpriteRenderer>().sprite.name
But that will only return the sprite used for the main GameObject's SpriteRenderer, and the name of it would not change even as animation changes it in Game Mode

GetComponent<SpriteRenderer>().sprite.name
This 100% does work , just tried it now and it is changing the name for each current sprite that the animator is changing.
You need to give more information and maybe a video since there is a problem somewhere else.

Related

How to adjust character height when playing seated in closed VR with MRTK?

I am creating a VR application with mrtk 2.1 and want to have the User on the same height when playing seated or standing.
When Playing seated the camera is too low.
I could adjust the height of the MixedRealityPlayspace which contains the MainCamera, but that resets after using the teleport function.
Additionally learned from this GitHub Post, that there is an Offset between the MainCamera and MixedRealityPlayspace which is not accessible via mrtk so calculating the right height would be difficult.
This just does not seem like the right approach.
How can I make sure that the User is always on the right head height in game even when playing seated?
I've gone ahead and filed the following feature request:
https://github.com/microsoft/MixedRealityToolkit-Unity/issues/6786
An untested workaround may be the following:
Create logic that instantiates a new GameObject at runtime.
Set the new GameObject's parent to be the MixedRealityPlayspace.
Set the new GameObject to be the parent of your main camera
Apply your desired offset as a transform to this new GameObject.
Talking with different MRTK Contributors, its felt that you may need to do this transform configuration at runtime. Trying to do this in the editor/in the scene hierarchy may encounter problems because folks were unsure whether or not the main camera's parent gets set as the MixedRealityPlayspace regardless of whether or not the main camera is a grandchild of the MixedRealityPlayspace.
OK i found a solution that works for me.
I use the following code to move the mixedRealityPlayspace up or down to achieve the wanted player height.(if you want to use this make sure to tag your mixedRealityPlayspace GameObject "Player")
var mixedRealityPlayspace = GameObject.FindGameObjectsWithTag("Player")[0].transform;
var camHeight = mainCamera.transform.position.y;
var adjustement =wantedPlayerHeight-camHeight ;
mixedRealityPlayspace.position= new Vector3(mixedRealityPlayspace.position.x,mixedRealityPlayspace.position.y+adjustement, mixedRealityPlayspace.position.z);
I call this on ones on the start of my Scene and in the OnTeleportCompleted Event described in this Dokumentation for the MRTK Teleport system

How to prefab animations in Unity?

created a prefab for door and added the prefab doors to the scene. This prefab door has an animator. The problem is when I try to change the animator x,y,z position on the second door in the scene it changes the first door.
I tried duplicating the doors and changing the position x y z but it changed the animator for the first door.
I just have a script that play the animator.
The expected result should be that I can drag and drop doors as needed without creating individual animators for each door. It looks like i need to create animators per door there is and also this probably will require me to create new scripts per each door. It seems awkward since I should be able to add doors and just chaning the position x y z in the animator.
The problem is that their Animator component uses the same AnimatorController asset which uses certain AnimationClip asset references for it's states.
I don't know if there is an easier way for achieve but I would do the following:
For what you want you would have to copy the AnimatorController asset (CTRL+D) as well as the AnimationClip asset you want to change.
Then in the cloned AnimatorController click on the according State you want to change and in the Inspector reference the according cloned AnimationClip.
Now you have a copy of the AnimatorController using a different AnimationClip but with the same states, traditions etc. so you can edit both animations and controllers individually.
finally in the Animator component reference the according AnimatorController for each prefab.
The remaining problem maybe is now that for the future if you change the States, transitions and especially parameters you have to do it in both AnimatorController assets individually.
That is perhaps not because of an animator, but a specific behavior of animation in Unity. Animation animates transform property according to parent, as far as I understand. So the first parent of your prefab is captured, and its transform property is used for animation. But I'm not exactly sure about this explanation - the actual behavior is covered under the Unity's source code. A common solution for this that I found is to create a parent or container inside your object's prefab. I don't know why it works, but it works. Perhaps because the parent of your animated object is now always the same.
Parent can be just an empty game object. So you make your door prefab, place another empty game object into this prefab, make your door a child of this container, set 0 for all coordinates of your door according to container and for the container itself. Now you can add an animator component to your object, which you want to animate (not the container), and animate it inside the prefab scene. After that you can place the copies of this prefab (container + your animated object inside) wherever you want inside your game scene - all animations of copies will proceed independently.

Unity - How to change player spritesheet while runtime?

I'm currently working on a 2D pixel Jump'n'Run. I want the player to be able to "buy" new skins for the player-character. I have multiple sprite-sheets. They all have the same structure. I'm using sprite animations.
How can I change the sprite-sheet at runtime? I found the following solution, but it's very resource intense: https://youtu.be/HM17mAmLd7k?t=1818
Sincerly,
Julian
The reason it's so resource intensive in the video is because the all the sprites are loaded in each LateUpdate(), which is once per frame. The script looks like it's grabbing all the sprites in the sprite-sheet and loading them every frame so that if the spriteSheetName ever changes, it will update the renderer on the next frame.
I don't believe that's necessary and in the video he mentions that it's just being used as an example. What I'd do is move it out of the LateUpdate() method and into its own method that can be called only whenever the user wants to change the sprite-sheet. So instead of mindlessly loading the sprites from the sprite-sheet each frame, you'll only load them whenever the user selects it.
That should accomplish drastically cutting down the intensity of this script because you're not loading all the sprites in a sprite-sheet and looping through each of their renderers on every single frame.

Unity 5.3.8 Animator - Glitch?

I created a bunch of non-AI animations for enemies that do basic back.forth or up/down motions. Everything was working perfect until I started working on the Boss of the level. The boss has his own tag "Boss" and enemies have their own tag as well. Anyway, the problem is when I click start, every enemy leaves the game board. I can see them animated above the game board still doing their routines.
Any clue as to why this happened and how to fix it? I'd really, really hate to have to scrap all the enemies and start from scratch...
I used the Animation tool inside unity.
Extra Note: I created an EMPTY and moved all of my enemies into that Empty object to clean the hierarchy panel up. The animations were fine before this.
************** Currently Resolved **************
Whew! Okay, so apparently Unity doesn't like it when you move your animated stuff into an empty AFTER being animated. I FIXED them by simply removing them from the EMPTY that I had placed them in. However, I'd still like to know why this is. So any useful resources, links, manuals, personal insight/observations or anywhere I can read up on this would be appreciated!
Here is an explanation what happened:
The moment you dragged them all into the empty GameObject I guess this object probably wasn't placed on 0,0,0 in the Scene.
So Unity automatically changed all the local position values of your enemy items to fit the current position offset to the empty object.
Result: In the editmode they don't change their actual global position in the scene but their local position. This is supposed to happen if you just want to organize stuff.
However, now when you start the game and the animations are played, the animators change all the local positions to whatever is stored in your animations.
Result: all objects jump back to their original localPosition which had an offset to the empty GameObject.
To solve this make sure the empty GameObject is at position 0,0,0 and optimally has rotation 0,0,0 and scale 1,1,1 before you drag anything into it.
Easiest way to achieve that is by clicking reset in the empty objects Transform component before starting to drag stuff into it.

[Unity3D]How to get reference to a specific Animation Clip in Animator

For the past 2 hours I have been searching everywhere and I can't find any resources to help me. The documentation is confusing (Especially for Animation) and every information I find is for a previous version of Unity that had a different animation system. Anyway, I have a GameObject with a Animator component attached. The Animator Controller has 4 animation states that it uses. I have a script attached to my GameObject and I can'd find out how to do the following:
1) Get the length any specific clip
2) Change the speed of any specific clip.
animator.speed = (some number) does not work because it changed the speed of ALL the clips. I want to change the speed of specific clips during runtime. Is there a way I can search the animation clip by name and change the clip speed? Or is there a way I can make a public AnimationClip, drag and drop the clip, and then edit the speed of the clip? And I want to change all of this during runtime, I know I can click on the Animation Clip and change the speed that way. Also, as I listed earlier, I need to get the length of the animation clip. I assume if I can change the speed of an animation clip, I can get the length of the clip because I'd have the reference to it. Any Help?
Use:
AnimationUtility.GetAnimationClips(GameObject)
By using this you will get all animationsClips in array from current gameObject. Then you will have everything you want.
See docs:
AnimationUtility.GetAnimationClips
AnimationClip