Search Unity

Script not sending info to animator

Discussion in 'Animation' started by padams73, Mar 20, 2014.

  1. padams73

    padams73

    Joined:
    May 20, 2013
    Posts:
    1
    Hi there, I am trying to code some basic walking and running animations on a third-person character. I have set up the animator with a parameter called "state", where a value of zero means play the idle animation, value of 1 means play walk, 2 means play running, 3 should play jumping. However, the script I have only plays the jumping animation successfully. When I move in any direction the character moves but the animations do not play. I have watched the animator while playing and the only time information gets sent to it is when I hold down controls for both vertical and horizontal movement at the same time. In other words, when moving diagonally the animations play. I keep looking at the code and I think the logic is right, and when I debug the if statements they are all working correctly, it's just the the Mek.SetInteger part isn't firing. Code it below, I would love it if anyone could suggest a fix! Thanks.

    function Update () {
    Debug.Log(walking);
    var Mek: Animator = GetComponent(Animator);
    // Movement controls
    // Can change input settings under Edit > Project Settings > Input
    if(Input.GetAxis("Vertical") > 0) {
    walking = true;
    if(Input.GetButton("Shift")) {
    // Use the * Time.deltaTime to convert from speed per frame to speed per second
    charController.Move(transform.forward * runSpeed * Time.deltaTime);
    state = 2;
    Mek.SetInteger("state", 2);

    } else {
    state = 1;
    Mek.SetInteger("state", 1);
    charController.Move(transform.forward * walkSpeed * Time.deltaTime);
    }
    }
    else if(Input.GetAxis("Vertical") < 0) {
    walking = true;
    Mek.SetInteger("state", 1);
    if(Input.GetButton("Shift")) {
    state = 2;
    Mek.SetInteger("state", 2);
    charController.Move(transform.forward * -runSpeed * Time.deltaTime);
    } else {
    state = 1;
    Mek.SetInteger("state", 1);
    charController.Move(transform.forward * -walkSpeed * Time.deltaTime);
    }
    } else {
    walking = false;
    //Mek.SetInteger("state", 0);
    }
    if(Input.GetAxis("Horizontal") > 0) {
    walking = true;
    if(Input.GetButton("Shift")) {
    state = 2;
    Mek.SetInteger("state", 2);
    charController.Move(transform.right * runSpeed * Time.deltaTime);
    } else {
    charController.Move(transform.right * walkSpeed * Time.deltaTime);
    }
    }
    else if(Input.GetAxis("Horizontal") < 0) {
    walking = true;
    if(Input.GetButton("Shift")) {
    charController.Move(transform.right * -runSpeed * Time.deltaTime);
    } else {
    charController.Move(transform.right * -walkSpeed * Time.deltaTime);
    }
    } else {
    walking = false;
    //Mek.SetInteger("state", 0);
    }


    // Jump control
    if (Input.GetButton("Jump") !jumping) {
    state = 3;
    Mek.SetInteger("state", 3);

    startJump();
    }
    if (jumping) {
    charController.Move(transform.up * gravityFall * Time.deltaTime);
    gravityFall -= gravity * Time.deltaTime;

    if(charController.isGrounded) {
    jumping = false;
    state = 0;

    }
    }
    if(!walking !jumping !preparingJump) {
    Mek.SetInteger("state", 0);
    }
    // Rotation controls
    transform.Rotate(Vector3(0, Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime, 0));
    }
    function startJump() {
    preparingJump = true;
    yield WaitForSeconds(0.3);
    preparingJump = false;
    gravityFall = jumpForce;
    jumping = true;
    }