Search Unity

Changing motion of start state of Controller in runtime

Discussion in '2D' started by vdarth722, Oct 13, 2022.

  1. vdarth722

    vdarth722

    Joined:
    Aug 12, 2022
    Posts:
    2
    I am working on a 2D game in which dungeons are generated in runtime. Now each room in the dungeon can have door at North, South, East or West randomly.

    If the player is exiting Room1 from South, the next scene should have the player in Room2 near the North door with Direction facing the room.

    I am able to achieve all the steps above except facing the room dynamically.
    If player exits Room1 from east, player should be facing east of Room2 and so on.

    I have Controller created for movement with a Default state But right now the Default state has a Motion that is assigned from inspector.

    How do I assign Motion to this Default on runtime? I have tried AnimatorOverrideController but there is no change in Motion field.

    The Controller:
    Controller.PNG Default state.PNG

    My PlayerMovement script handles the animation of the Player

    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour
    2. {  
    3.         public AnimationClip[] clip;
    4.  
    5.     protected AnimatorOverrideController animatorOverrideController;
    6.  
    7.     void Start()
    8.     {
    9.         dir = GetDirection();
    10.         Debug.Log("Dir is " + dir);
    11.        
    12.         animatorOverrideController = new AnimatorOverrideController(animator.runtimeAnimatorController);
    13.         animator.runtimeAnimatorController = animatorOverrideController;
    14.         animatorOverrideController["Default"] = clip[dir];
    15.     }
    16. }
    Player inspector:
    Player inspector.PNG

    Thanks in advance.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Right now you have the player "picking up" the position of the new entrypoint in the next room... best way is always to obtain that from an empty GameObject (via Transform) that you inject into the created level when you're done.

    Then it is easy to rotate that invisible object to face "into" the room, and have the player "pick up" rotation as well as position.

    Also, +1 for procedural dungeon game. :)