Search Unity

Can't get my animations to work properly via triggers/script

Discussion in 'Animation' started by DustyShinigami, Apr 18, 2019.

  1. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    I'm currently making a small 2.5D platformer as part of a college project, and I want to use the scripts I made from another prototype I made. I made the character, texture, a simple pixel background and a couple of animations, but I can't seem to get the triggers to work to move the player and for the animations to work properly. I've looked back at my previous prototype to see if everything matches up in the animation's parametres, the console hasn't given me any errors, I've put the Animation Controller in the correct box in the Inspector etc. but they aren't working. I'm not sure what I'm overlooking. The isGrounded tick box doesn't get marked either. Nor the Jumped box in the Inspector, so these triggers aren't working for some reason.

    This is the PlayerController script I have:

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     public Animator anim;
    4.     public float moveSpeed;
    5.     public float jumpForce;
    6.     public bool jumped;
    7.     public float gravityScale;
    8.     public float knockBackForce;
    9.     public float knockBackTime;
    10.     public float invincibilityLength;
    11.  
    12.     private float jumpDelay;
    13.     private Vector3 moveDirection;
    14.     private float knockBackCounter;
    15.     private float invincibilityCounter;
    16.     private CharacterController controller;
    17.  
    18.     void Start()
    19.     {
    20.         Cursor.visible = false;
    21.         controller = GetComponent<CharacterController>();
    22.         jumped = false;
    23.         if(jumpDelay <= 0)
    24.         {
    25.             jumpDelay = 5;
    26.         }
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         if (knockBackCounter <= 0)
    32.         {
    33.             float moveHorizontal = Input.GetAxis("Horizontal");
    34.             moveDirection = new Vector3(moveHorizontal * moveSpeed, moveDirection.y);
    35.  
    36.             if (moveHorizontal > 0)
    37.             {
    38.                 transform.eulerAngles = new Vector3(0, 90);
    39.             }
    40.             else if (moveHorizontal < 0)
    41.             {
    42.                 transform.eulerAngles = new Vector3(0, -100);
    43.             }
    44.  
    45.             if (controller.isGrounded)
    46.             {
    47.                 moveDirection.y = -1f;
    48.                 if (Input.GetKey(KeyCode.KeypadPlus))
    49.                 {
    50.                     moveDirection.y = jumpForce;
    51.                     jumped = true;
    52.                     StartCoroutine(SpamBlockco());
    53.                 }
    54.             }
    55.         }
    56.         else
    57.         {
    58.             knockBackCounter -= Time.deltaTime;
    59.         }
    60.  
    61.         moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
    62.         controller.Move(moveDirection * Time.deltaTime);
    63.  
    64.         anim.SetBool("isGrounded", controller.isGrounded);
    65.         anim.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Horizontal"))));
    66.  
    67.     }
    68.  
    69.     public void Knockback(Vector3 direction)
    70.     {
    71.         knockBackCounter = knockBackTime;
    72.  
    73.         moveDirection = direction * knockBackForce;
    74.         moveDirection.y = knockBackForce;
    75.     }
    76.  
    77.     public IEnumerator SpamBlockco()
    78.     {
    79.         if (moveDirection.y == jumpForce)
    80.         {
    81.             yield return new WaitForSeconds(jumpDelay);
    82.         }
    83.         yield return null;
    84.         jumped = false;
    85.     }
    86. }
    Here are some screenshots to compare the two. Some things I haven't enabled or bothered with as I don't believe they're important at the moment. But correct me if I'm wrong. I don't have a run animation yet as I haven't made it. :)
     

    Attached Files:

    Last edited: Apr 18, 2019
  2. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    Final image:
     

    Attached Files:

  3. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    No one? Or would I have been better posting this in the scripting forum?
     
  4. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    I've figured some of it out through experimenting, but it just doesn't recognise or check the box for isGrounded. My idle animation doesn't reloop either once the cycle finishes. I don't understand why as my character has a Character Controller, a Sphere Collider a Rigidbody and the platform/cube has a Box Collider.
     
  5. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Put in some Debug.Log calls to figure out what is actually happening.

    Your transitions look fine at a glance but it's hard to actually tell. The whole transition system is stupid because it adds a totally unnecessary layer of abstraction between your code saying something should happen and that thing actually happening.