Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Animations won't transition

Discussion in 'Animation' started by BevvyofFun, Sep 5, 2020.

  1. BevvyofFun

    BevvyofFun

    Joined:
    Aug 19, 2020
    Posts:
    25
    Hi, as a newbie to programming, but working hard to get things going, I don't know why my animations won't transition, although I've built transitions into each animation within the Animator.
    Can you see any reason this code won't make the transitions? Please help!
    This is my code:

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

    public class LandonController : MonoBehaviour

    {

    Animator animator;




    // Start is called before the first frame update
    void Start()
    {
    animator = GetComponent<Animator>();

    }


    // Update is called once per frame
    void Update()
    {

    float horizontal = Input.GetAxis("Horizontal");
    float vertical = Input.GetAxis("Vertical");


    bool KeyUp = (Input.GetKeyDown(KeyCode.UpArrow));
    bool KeyUpUp = (Input.GetKeyUp(KeyCode.UpArrow));
    bool KeyDown = (Input.GetKeyDown(KeyCode.DownArrow));
    bool KeyDownUp = (Input.GetKeyUp(KeyCode.DownArrow));
    bool KeyRight = (Input.GetKeyDown(KeyCode.RightArrow));
    bool KeyRightUp = (Input.GetKeyUp(KeyCode.RightArrow));
    bool KeyLeft = (Input.GetKeyDown(KeyCode.LeftArrow));
    bool KeyLeftUp = (Input.GetKeyUp(KeyCode.LeftArrow));





    //add animations to each key position,
    //write a function that sets the conditions for transition of animations, then call //the function

    void LandonWalk() {

    if (KeyUp = true)
    {
    animator.SetTrigger("WalkUp");
    Vector2 position = transform.position;
    position.x = position.x + 3.0f * horizontal * Time.deltaTime;
    position.y = position.y + 3.0f * vertical * Time.deltaTime;
    transform.position = position;



    }


    //Detect when the up arrow key has been released

    else if (KeyUpUp = true)
    {
    Debug.Log("Up Arrow was released");


    }

    else if (KeyDown = true)
    {
    animator.SetTrigger("WalkDown");
    Vector2 position = transform.position;
    position.x = position.x + 3.0f * horizontal * Time.deltaTime;
    position.y = position.y + 3.0f * vertical * Time.deltaTime;
    transform.position = position;





    }

    //Detect when the up arrow key has been released

    else if (KeyDownUp = true)
    {
    Debug.Log("Down Arrow was released");



    }

    else if (KeyRight = true)
    { animator.SetTrigger("WalkRight");
    Vector2 position = transform.position;
    position.x = position.x + 3.0f * horizontal * Time.deltaTime;
    position.y = position.y + 3.0f * vertical * Time.deltaTime;
    transform.position = position;



    }

    //Detect when the up arrow key has been released

    else if (KeyRightUp = true)
    { Debug.Log("Right Arrow was released");



    }

    else if (KeyLeft = true)
    { animator.SetTrigger("WalkLeft");
    Vector2 position = transform.position;
    position.x = position.x + 3.0f * horizontal * Time.deltaTime;
    position.y = position.y + 3.0f * vertical * Time.deltaTime;
    transform.position = position;


    }

    //Detect when the up arrow key has been released
    else if (KeyLeftUp = true)
    { Debug.Log("Left Arrow was released");



    }


    }
    LandonWalk();

    }



    }
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,497
    You have code like
    if (KeyUp = true)
    which doesn't check the value, it assigns it and always returns true. What you want is
    if (KeyUp)
    , which means the same as
    if (KeyUp == true)
    .. note the "==" compares whether equal, whereas "=" sets a value.
     
    BevvyofFun likes this.
  3. BevvyofFun

    BevvyofFun

    Joined:
    Aug 19, 2020
    Posts:
    25
    Thank you!! That made it read the code, but there must be something wrong in the Animator, because it still doesn't transition to the animations. It only shows one animation and stays there. I'll keep working at it. Thanks again!
     
    adamgolden likes this.
  4. BevvyofFun

    BevvyofFun

    Joined:
    Aug 19, 2020
    Posts:
    25
    Woohoo!!! It finally all works!!! Just had to tweak a bit on the Animator. Can't thank you enough! Just a little ==!
     
    adamgolden likes this.
  5. BevvyofFun

    BevvyofFun

    Joined:
    Aug 19, 2020
    Posts:
    25
    You were such a great help before, I hope you can give me a little assistance on this one. This code has no errors but a warning that the method is declared never used. I must be missing something to implement the method. It's a trigger I'm trying to implement for further functionality.

    Thanks for any help you can give!

    Bev

    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.SceneManagement;
    7.  
    8.  
    9.  
    10.  
    11.  
    12. public class LivingRoomDoor : MonoBehaviour
    13. {
    14.    
    15.    
    16.     public enum anyDoor{LivingRoomDoor};
    17.    
    18.    
    19.    
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.    
    25.    
    26.        
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.    
    33.    
    34.  
    35.         //call function under certain circumstances, if statement
    36.    
    37.     //call the function
    38.  
    39.         void OnTriggerEnter(Collision2D trigger2D)
    40.             {
    41.         if (trigger2D.transform.name == "LivingRoomDoor")
    42.     {
    43.         LandonController player = gameObject.GetComponent<LandonController >();
    44.  
    45.         if (player != null)
    46.             {
    47.        
    48.  
    49.                 Debug.Log("Landon touched the livingroom door.");
    50.      
    51.      
    52.     } else {
    53.  
    54. Debug.Log("Can't find Landon");
    55. }
    56.  
    57. }
    58. }
    59.  
    60. }
    61. }
    62.  
     
  6. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,497
    The warning is because Start has no code in it - you can comment that out until you're ready to use it (or delete it if you don't plan to).
    Code (CSharp):
    1. /*
    2.     void Start()
    3.     {
    4.     }
    5. */
    or
    Code (CSharp):
    1.   // void Start()
    2.   //  {
    3.   //  }
     
    BevvyofFun likes this.