Search Unity

animation script help

Discussion in '2D' started by Justadadgaming, Jan 21, 2020.

  1. Justadadgaming

    Justadadgaming

    Joined:
    Jan 21, 2020
    Posts:
    7
    I have the sprite animations done for idle, walk left, walk right, jump left, jump right. With bools set to facingRight, isWalking, isJumping connecting the animations. With the jumping connecting to any state. For the script i have this :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PlayerMove : MonoBehaviour
    4. {
    5.     public float moveSpeed;
    6.     public float jumpHeight;
    7.     private bool isGrounded;
    8.     // Update is called once per frame
    9.     void Update()
    10.     {
    11.         Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
    12.         transform.position += move * moveSpeed * Time.deltaTime;
    13.         if (Input.GetKeyDown("w") && isGrounded)
    14.         {
    15.             GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpHeight), ForceMode2D.Force);
    16.             GetComponent<Animator>().SetBool("isJumping", true);
    17.             StartCoroutine(JumpAnim());
    18.         }
    19.         if (move.x > 0 && isGrounded)
    20.         {
    21.             GetComponent<Animator>().SetBool("isWalking", true);
    22.             GetComponent<Animator>().SetBool("facingRight", true);
    23.      
    24.         }
    25.         if (move.x < 0 && isGrounded)
    26.         {
    27.             GetComponent<Animator>().SetBool("isWalking", true);
    28.             GetComponent<Animator>().SetBool("facingRight", false);
    29.          
    30.         }
    31.         if (move.x < 0)
    32.         {
    33.             GetComponent<Animator>().SetBool("facingRight", false);
    34.         }
    35.         else if (move.x > 0)
    36.         {
    37.             GetComponent<Animator>().SetBool("facingRight", true);
    38.         }
    39.         if (move.x == 0 || isGrounded == false && GetComponent<Animator>().GetBool("isJumping") == false)
    40.         {
    41.             GetComponent<Animator>().SetBool("isWalking", false);
    42.         }
    43.     }
    44.     IEnumerator JumpAnim()
    45.     {
    46.         yield return new WaitForSeconds(0.333f);
    47.         GetComponent<Animator>().SetBool("isJumping", false);
    48.     }
    49.     void OnTriggerEnter2D()
    50.     {
    51.         isGrounded = true;
    52.     }
    53.     void OnTriggerStay2D()
    54.     {
    55.         isGrounded = true;
    56.     }
    57.     void OnTriggerExit2D()
    58.     {
    59.         isGrounded = false;
    60.     }
    61. }
    62.  
    and i did add w as a key for jump in settings
    Now the character moves left and right. However the walking animations wont trigger and it does not jump at all. Could use some help.
     
    Last edited: Jan 21, 2020
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  3. Justadadgaming

    Justadadgaming

    Joined:
    Jan 21, 2020
    Posts:
    7
  4. Justadadgaming

    Justadadgaming

    Joined:
    Jan 21, 2020
    Posts:
    7
    is it possible its not finding the sprite grounded?
    i have the base of the level set up with a edge collider, and the main sprite has a box collider and rigid body
     
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    You arent using the OnTriggerEnter2D or OnTriggerStay2D properly.
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D col)
    2. {
    3.     if(col.gameObject.tag == "Ground")
    4.     {
    5.         isGrounded = true;
    6.     }
    7. }
    Check that out or look at the documentation. You need to do the same for OnTriggerStay2D and Exit2D.
     
  6. Justadadgaming

    Justadadgaming

    Joined:
    Jan 21, 2020
    Posts:
    7
    Did as stated and now getting a compiler error:
    UnityEditor.SceneView:ShowCompileErrorNotification() (at C:/buildslave/unity/build/Editor/Mono/SceneView/SceneView.cs:3398)
     
  7. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    Well did you set up a tag called "Ground" or anything? And after setting up the tag did you assign it to the ground gameobjects? Mine was just an example.
     
  8. Justadadgaming

    Justadadgaming

    Joined:
    Jan 21, 2020
    Posts:
    7
    yes i have my foreground layer tagged as Ground layered as ground
     
  9. Justadadgaming

    Justadadgaming

    Joined:
    Jan 21, 2020
    Posts:
    7
    ok reloaded into unity, to reload the project with that added in place of original. sprite still only moves left and right stuck in idle animation no jumping. In the animator tab i have the animations loaded correctly with bools going to each one for isWalking,facingRight,isJumping. In the preview the animations work great, just in the game window nothing changes from moving left and right with only the idle animation.
     
  10. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    Do you have your transitions set up within the animator properly?
     
  11. Justadadgaming

    Justadadgaming

    Joined:
    Jan 21, 2020
    Posts:
    7
    i believe so from idle to walk right has booles isWalking true and facingRight true. from idle to Jumping Right has booles isJumping true and facingRight true etc
     
  12. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    Man, I wish i could help more but i am not sure. Try looking for typos or small things. That error you sent doesnt really explain much, Im sure someone else may know more but without more details on the error, I am afraid I am not sure.