Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. If you're a macOS 10.13 High Sierra user take be sure to read this before updating Unity.
    Dismiss Notice
  5. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  7. Unity 2017.3 beta is now available for download.
    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:
    2,637
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    5,898
    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.)