Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. 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,082
  3. JoeStrout

    JoeStrout

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