unit – How to animate two or more unbound properties on a GameObject sprite using animation layers?

I have a sprite in Unity 2019.3. It is animated like this (with the models provided):

These animations are all independent of each other and can all occur at any time. For example, when an armored player takes a hit, his shield degrades but he also gains a brief invincibility. It would look like this:

A single state machine to account for these possibilities (and others, depending on whether or not I add other power-ups or mechanics) would be prohibitively complex . So I'm going with layers of animation.

I was hoping it would be as simple as adding separate animation layers with their own state machines and then marking each layer as an additive. Nope. I have no idea what I am doing.

How can I use Mecanim to animate my sprite as I previously described?

unit – How to instantiate a new GameObject at the position of the old GameObject onAnimationComplete

I have a GameObject which is a farmer who plants seeds. The seeds will grow into plants. Once this animation is over, I would like to replace my seed with a plant. This happens hundreds of times per game at arbitrary positions. I tried this as something that I built from this question: Unity: detect the end of the animation

public class SeedScript : StateMachineBehaviour {
(SerializeField) private GameObject seed;
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    Instantiate (tree, this.position,  this.rotation);


Obviously, it doesn't work at all. Don't even compile.

I also looked at these co-routines: https://answers.unity.com/questions/981948/statemachinebehaviour-startcoroutine.html but again, I need to get some data position of the animation object.

Yes, I could just merge my Seed and Plant classes together, suppose I can't do it and MUST instantiate a new arbitrary object at the position of another object at the end of an animation.

unit – null reference on gameObject that was successfully referenced

It is exactly as the title says. I receive a null reference error even if I have correctly referenced the object. I have debugged my code to show that the result is still exactly what I need, but each time it is put into practice, the code returns an error.

                if(hit == true && canSee.transform.CompareTag("Unit"))
                    //var Obehaviour = other.gameObject.transform.parent.gameObject.GetComponent().Health; <--- Error is here.
                    float Health = other.gameObject.transform.parent.gameObject.GetComponent().Health;
                    float fullHealth = other.gameObject.transform.parent.gameObject.GetComponent().MaxHealth;
                    //Debug.Log("Object " + other.gameObject.transform.parent.gameObject);
                    //Debug.Log("Component and name " + Obehaviour.Name);
                    if(Health < fullHealth)
                        if(fullHealth - Health < Ubehaviour.supplies)
                            Health += Ubehaviour.supplyTransferRate;
                            Health = Mathf.Clamp(Health, 0, fullHealth);
                            Ubehaviour.supplies -= Ubehaviour.supplyTransferRate;
                            Ubehaviour.supplies = Mathf.Clamp(Ubehaviour.Health, 0, Ubehaviour.maxSupplies);


As stated in the code above, I tried to get the values ​​I need by storing them in variables other than caching the entire class. But it didn't work either. What's wrong? The debugging below also works perfectly.

How can I fix a GameObject along an axis only when it is part of another object in Unity?

Hello, I have an Object player and inside the player I have a Boundary object which is supposed to follow the player when it goes up or down only? (When the player moves to the right or to the left, I don't want the border object to move. How can I do this?

3D – Unity – Use mesh rendering to display specific sprites controllable by GameObject instead of hardware

I'm trying to replace sprite renderings in a 3D environment, but I can't seem to find a way to control the texture of mesh rendering per game object as it works in sprite rendering. I can't use sprite renderers, because using LWRP disrupts sorting and they intermingle and separate constantly. I don't want to have 100 different materials that are functionally the same as the texture, is there a way to do it?

quaternion – Calculate the orientation of GameObject in the custom coordinate system in Unity

I'm trying to build a custom coodinate system. To be more precise, the user chooses three points on the screen to define the coordinate system. The first is the origin, the second is used to build the OX axis, the third OZ and the last OY. The system is properly constructed. The axes are perpendicular to each other.

Once this coordinate system is built, I need to convert the orientation and position of objects placed on the screen from world space to this particular world space. To do this, I tried the solution proposed from here 1. It seems to work, but only for the post. The orientation seems not to change at all.

For example, if I place a cube at (0, 0, 0) without rotating it in world space, in my own coordinate system, it must also be at (0, 0, 0) and it does not should not be rotated relative to my own coordinate system. So the cube in my coordinate system should have a rotation relative to the world space. However, in reality, my code places the cube at my own origin, but does not rotate it accordingly.

In the image below, the first coordinate system (the smallest) is my own system. The other is of world origin. We can see that the WorldObject cube is placed at (0, 0, 0) with rotation (0, 0, 0). The OwnObject should be the representation of WorldObject in my system. We can see that the position has changed, but not the rotation.

My question is how can I rotate the second cube accordingly? I tried to define the rotation of the cube to be the orientation of the structure proposed in this solution 1, but it did not seem to rotate correctly. I also tried to use Quaternion.LookRotation, but it didn't work either.

enter description of image here
1 How to create a custom set of coordinates based on two points in Unity?

c # – Unity gets the GameObject reference under another GameObject

I'm trying to get the reference of the object located under my drive.

I do not know if I approach this properly. For the moment, I just have a player who puts himself on the stage, like this: Player normally in the scene

Then I try to get a reference like that (it does not work)

public void CheckTileUnder()
    Vector3 halfExtents = new Vector3(0f, -1 / 2.0f, 0f);
    Collider() colliders = Physics.OverlapBox(transform.position, halfExtents);

    foreach (Collider item in colliders)
        Tile tile = item.GetComponent();
        if (tile != null)
            RaycastHit hit;

            if (Physics.Raycast(tile.transform.position, Vector3.down, out hit, 1))
                currentTile = tile;

Another approach that I thought would be to place the player under the tile like this: so I guess I should change parents as suggested in one of the answers here?
Player under tile

How should I select the tile under the player?

unit – Why do boxCollider-related scopes change when changing the size of a gameObject but not in the script?

So, I have two players when I made them for the first time, they were with the default size Y = 1. Once the scripts finished, I wanted to change their size, but the calculations use the size of Y = 1 and I do not understand why is it.

The code I'm running is:

public class Player1Controller : PlayersController {
    private Vector3 downMost;
    private Vector3 upMost;
    private float minY, maxY;
    private float halfHeight;

    // Start is called before the first frame update
    private void Start () {
        halfHeight = GetComponent ().sprite.bounds.extents.y;

        float distance = transform.position.z - Camera.main.transform.position.z;
        downMost = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance));
        upMost = Camera.main.ViewportToWorldPoint (new Vector3 (1, 1, distance));

        minY = downMost.y + halfHeight;
        maxY = upMost.y - halfHeight;

    // Update is called once per frame
    private void Update () {
        float verticalMovement = Input.GetAxis ("Player1Vertical");
        MovePlayer (verticalMovement, transform);
        PlayerLimit (transform, minY, maxY);

The PlayerLimits method is

public void PlayerLimit (Transform transform, float minY, float maxY) {
        float restrictY = Mathf.Clamp (transform.position.y, minY, maxY);
        transform.position = new Vector3 (transform.position.x, restrictY, transform.position.z);

I guess the problem is either in the sprite bounds.extents.y or the restrictY but I dont understand.

This helps me with the limitations of the player, shown in the following image:
Limit of the players

As you can see on the left side, the modified drive has a size less than 1 and on the right side the drive with the default size, the script is the same, but the calculations are wrong. The left bar had to go to the end of the border, but that is not the case.

unit – Easily save and load the complete state of a GameObject in Unity3D?

I'm working on place predefined tilemap rooms in my procedural dungeonsand I would like design the location of objects and monsters in these special rooms as well as.

Currently I "compile" my preconceived room scene in scriptable object, and the map generator loads this tilemap in tilemap "gameplay".

I'm already analyzing the special game objects in the scene (like objects and monsters), and store their transformations and prefabricated roads in the scriptable object.

But this has 2 problems:

  • Have each object and monster in the resource folder would make my executable huge.

    • But that could be solved by doing a "MonsterPrefabDictionary" on a manager like gameobject, and drop each prefab into this list in the editor. The map generator will simply use this dictionary to find the prefab of the object.
  • the bigger problem Is each monster variant would need another prefab (or prefabricated variant). And by variation, I mean also small changes.

    • Do you want a crowd to have only 50% health in the room early? Make a prefab for that?

    • You want a monster to start with a specific buff in this room? Make a prefab for that?

    • Do you want a monster to have a different position from the usual one in this room? Make a prefab for that?

    • As you can see, it weighs badly.

    • But that could be solved by store all MonsterScript data in the object that can be the subject of a script.

    • But that would be bad scale toobecause each new information about a monster should also be added to this serialization process.

is there integrated, automatic, easy for just register the complete state of a GameObject and just load everything with a single line later?

unit – Rotating the cube as a child of another gameObject causes it to tilt in 2 of the 3 axes

I added a cube and changed its scale and position but not its rotation.
I've added a second cube as a child of this first cuboid. I hoped to rotate it, but instead, it only skews the second cuboid.
Tilt only occurs in 2 of the 3 axes.
In addition, tilt does not occur if the cuboid child is not a child of the first cuboid.

Screenshots below. Skew happens in one of two axes:


Rotation occurring correctly in the third axis:


How do I turn it in all directions?
Also that Skew seems useful. How do you control tilt on all axes?