Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Animation Code Problem

Discussion in 'Scripting' started by TempestDemon, Nov 17, 2017.

  1. TempestDemon

    TempestDemon

    Joined:
    Oct 27, 2017
    Posts:
    2
    I've tried multiple hours on getting a code to work and I've looked everywhere on the internet. My problem is that my character in my "top down rpg" keeps flashing it's down-facing sprite for a splitsecond every time it stops walking. Everything looks fine aside from that but I just can't seem to figure it out.

    lastMove is used to keep the player facing the direction he was walking to. But everytime he flashes the down-facing sprite for a split second and I can't seem to figure out why.

    This is the code I have:

    using UnityEngine;
    using System.Collections;


    public enum Direction { Up, Down, Left, Right }

    public class GridMovement : MonoBehaviour {
    private bool canMove = true, moving = false;
    private int speed = 5, buttonCooldown = 0;

    private Direction dir = Direction.Down;
    private Vector3 pos;

    private Animator anim;

    private bool playerMoving;
    private Vector2 lastMove;


    // Use this for initialization
    void Start () {
    anim = GetComponent<Animator> ();
    }


    // Update is called once per frame
    void Update () {
    buttonCooldown--;

    if (canMove) {
    pos = transform.position;
    move ();
    }

    if (moving) {
    if (transform.position == pos) {
    moving = false;
    canMove = true;


    move ();
    }
    transform.position = Vector3.MoveTowards (transform.position, pos, speed * Time.deltaTime);

    }
    anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
    anim.SetFloat("MoveY", Input.GetAxisRaw ("Vertical"));
    anim.SetBool ("PlayerMoving", playerMoving);
    anim.SetFloat ("LastMoveX", lastMove.x);
    anim.SetFloat ("LastMoveY", lastMove.y);

    }

    private void move() {
    if (buttonCooldown <= 0) {
    if (Input.GetAxisRaw ("Vertical") > 0.5f) {
    if (dir != Direction.Up) {
    buttonCooldown = 4;
    dir = Direction.Up;
    } else {
    canMove = false;
    moving = true;
    pos += Vector3.up;
    playerMoving = true;
    lastMove = new Vector2 (0f, Input.GetAxisRaw ("Vertical"));
    }
    } else if (Input.GetAxisRaw ("Vertical") < -0.5f) {
    if (dir != Direction.Down) {
    buttonCooldown = 4;
    dir = Direction.Down;
    } else {
    canMove = false;
    moving = true;
    pos += Vector3.down;
    playerMoving = true;
    lastMove = new Vector2 (0f, Input.GetAxisRaw ("Vertical"));
    }
    } else if (Input.GetAxisRaw ("Horizontal") < -0.5f) {
    if (dir != Direction.Left) {
    buttonCooldown = 4;
    dir = Direction.Left;
    } else {
    canMove = false;
    moving = true;
    pos += Vector3.left;
    playerMoving = true;
    lastMove = new Vector2 (Input.GetAxisRaw ("Horizontal"), 0f);
    }
    } else if (Input.GetAxisRaw("Horizontal") > 0.5f) {
    if (dir != Direction.Right) {
    buttonCooldown = 4;
    dir = Direction.Right;
    } else {
    canMove = false;
    moving = true;
    pos += Vector3.right;
    playerMoving = true;
    lastMove = new Vector2 (Input.GetAxisRaw ("Horizontal"), 0f);
    }
    }
    }
    }
    }
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    3,342
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    7,277
    It's quite likely the problem isn't in the code at all, but in your animations. Check over your animations and animation controller carefully for that extra downward-facing frame. (And you may also want to consider this.)