0
\$\begingroup\$

I'm playing some animations through a C# script. In such script, I'm not using any other methods except GetComponent<Animation>() and Play(). The animations are of opening/closing car doors and are being played from separate, different functions.

When I open a new door, the last opened door goes back to its default state of being closed. How can I stop that from occurring? I want the doors to stay open until I close them again.

All doors and animations belong to a single object, which is a car model made by my coworker. It has one joint bone for each door. There's no skinned mesh renderer component.

Here's one of the functions:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class handle : MonoBehaviour
{
    public GameObject Car;
    private Animation anima;
    public bool bootspaceAnim = false;

    void Start()
    {
        anima = Car.GetComponent<Animation>();
    }

    public void bootspaceHandle()
    {
        if (bootspaceAnim == false)
        {
            anima.Play("Bootspace_Anim");
            anima["Bootspace_Anim"].speed = 1.0f;
            anima["Bootspace_Anim"].time = 0;
            
            bootspaceAnim = true;
        }
        else
        {
            anima.Play("Bootspace_Anim");
            anima["Bootspace_Anim"].speed = -1.0f;
            anima["Bootspace_Anim"].time = anima["Bootspace_Anim"].length;
            
            bootspaceAnim = false;
        }
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ Could you assign for each of your scripts the Animation Controller via the inspector? To make sure that Car.GetComponent<Animation>(); is not returning the same for each of your scripts. \$\endgroup\$
    – Zibelas
    May 12 at 15:48
  • \$\begingroup\$ Why you choose to use Animations directly instead of Animator / Animator Controller? \$\endgroup\$
    – Nikaas
    May 13 at 6:11
  • \$\begingroup\$ @Zibelas gonna try that out \$\endgroup\$
    – ahiyantra
    May 13 at 6:42
  • \$\begingroup\$ @Nikaas due to a lack of experience with it & not much time remaining to implement things \$\endgroup\$
    – ahiyantra
    May 13 at 6:43

2 Answers 2

1
\$\begingroup\$

This is what animation layers in the animator controller are for. Each animation layer is a separate state-machine. So it is possible for each layer to play a different animation at the same time. The controlled object will then play all the animations simultaneously.

Put the animations of each door (or any other sub-part of the object you want to animate independently) on an own animation layer. Then use animator.Play(state, layerNumber) to play a specific animation state on a specific layer.

\$\endgroup\$
0
\$\begingroup\$

Separating the scripts/controllers for all animations finally resolved the bugs. It seems that the animations interfere with each-other when they're manipulated using a shared script/controller.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .