Search Unity

The character cannot move when attached with animator controller

Discussion in '2D' started by samuelchow616, Aug 16, 2019.

  1. samuelchow616

    samuelchow616

    Joined:
    Jan 29, 2019
    Posts:
    2
    HI guys, I am a newbie in using unity2D. I encounter some problems when I try to move the character with UI buttons. I have created two buttons to move right and left and generate some simple animation linkage using the animator controller.

    However, when I play in the game scene, the character wont move according to the two buttons but only doing animation itself. Then I try to disable the animator attached on the character, although there are no animation, the buttons work which can control the movement of the character. I have created an empty game object called "frog_player" to hold all the sprites of the characters, and the scripts, collider and rigidbody are attached to the empty game object "frog_player". I have worked for weeks but cannot look for the solution even the problem itself.

    part of the files have been attached and really seek for any opinions to solve that, thank you .

    The script of the player is as follows:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class player_controller : MonoBehaviour
    6. {
    7.         public float jumpforce;
    8.         public float speedX;
    9.         public Transform groundcheck;
    10.         public float checkradius;
    11.         public LayerMask whatisgrounded;
    12.  
    13.         private bool facingright;
    14.         private bool isgrounded;
    15.         private float moveinput;
    16.         private bool jumping;
    17.         private Animator anim;
    18.         private Rigidbody2D rb;
    19.  
    20.         // Start is called before the first frame update
    21.         void Start()
    22.         {
    23.             anim = GetComponent<Animator>();
    24.             rb = GetComponent<Rigidbody2D>();
    25.             facingright = true;
    26.         }
    27.  
    28.         // Update is called once per frame
    29.         void Update()
    30.         {
    31.             Moveplayer(moveinput);
    32.             isgrounded = Physics2D.OverlapCircle(groundcheck.position, checkradius, whatisgrounded);
    33.  
    34.             if (isgrounded == true)
    35.             {
    36.                 anim.SetBool("isJumping", false);
    37.             }
    38.  
    39.             else
    40.             {
    41.                 anim.SetBool("isJumping", true);
    42.             }
    43.  
    44.            rb.velocity = new Vector3(moveinput, rb.velocity.y, 0);
    45.         }
    46.  
    47.         void Moveplayer(float moveinput)
    48.         {
    49.            
    50.             if (moveinput < 0 && !jumping || moveinput > 0 && !jumping)
    51.             {
    52.                 anim.SetBool("isRunning", true);
    53.                 flip();
    54.             }
    55.  
    56.             if (moveinput == 0 && !jumping)
    57.             {
    58.                 anim.SetBool("isRunning", false);
    59.             }
    60.            
    61.         }
    62.  
    63.         private void OnCollisionEnter2D(Collision2D collision)
    64.         {
    65.             if(collision.gameObject.tag == "Ground")
    66.             {
    67.                 jumping = false;
    68.             anim.SetBool("isJumping", false);
    69.             }
    70.         }
    71.  
    72.         void flip()
    73.         {
    74.             if (moveinput > 0 && !facingright || moveinput < 0 && facingright)
    75.             {
    76.                 facingright = !facingright;
    77.                 Vector3 scaler = transform.localScale;
    78.                 scaler.x *= -1;
    79.                 transform.localScale = scaler;
    80.             }
    81.         }
    82.  
    83.         public void walkleft()
    84.         {
    85.             moveinput = -speedX;
    86.        
    87.     }
    88.  
    89.         public void walkright()
    90.         {
    91.             moveinput = speedX;
    92.        
    93.     }
    94.  
    95.         public void stopwalking()
    96.         {
    97.             moveinput = 0;
    98.        
    99.     }
    100.     }
    101.  
     

    Attached Files:

  2. Ledesma099

    Ledesma099

    Joined:
    May 15, 2018
    Posts:
    6
    That happen because the animation is overwriting every change that velocity does. I was researching in forums and for solve this there are a way that is called StateMachineBehaviour script. In this script you can control the state of your animation and add physics. But for do this you need to change the Update Mode to Animate Physics in the Animator.

    Sorry for not giving a good answer
     
  3. samuelchow616

    samuelchow616

    Joined:
    Jan 29, 2019
    Posts:
    2
    Thank you very much Ledesma, that is really helpful for me already. I may get more research on using StateMachineBehaviour to learn more. I am not sure whether I have solved that or not, but after I disable the apply root motion and set the update mode to normal, it seems both the character and button can move accordingly.