[SOLVED] Help - Animating a Simple Sprite from Idle to Walk

Hi Guys,

This one has been driving me crazy. I’ve actually considered giving upon my game cause i cannot for the life of me figure out this simple concept (to most of you)

I have a Idle Animation - just 1 frame
I have a Walking Animation
I have an animation Controller
I haven’t setup any transitions in the animation controller because im confused - Except Default to Idle with no conditions. i have also placed the walking animation in there as well with no transition conditions.
I have named the animation “TrumpWalk” i have tagged the animation “TrumpWalk”
I’ve tried to call from script…

How in the hell do i get the animations to change on a keypress right to go from Idle to Walk
I’m getting null Exception Errors. I have actually seen many tutorials and other forum posts on this.
Am i really a complete dumb ass because I’m just not getting it.

Why do i need an animation controller at all, cant i just call states based on key inputs?
Because the animation controller wants Conditions and i have no idea how to call these or change from script. for example a bool that says if the player is idle or not and how the hell to implement it???

here is my crappy newb code.

using UnityEngine;
using System.Collections;

public class movement : MonoBehaviour
{
//Myline for adjusting the clouds layer to keyb to smoothen from parallax effects
public GameObject cloudlayer;
//public float cloudmovemaintain;
public float cloudoffset;
public float TrumpWalkSpeed;
public int TrumpDirection = 0;
public GameObject trump;
Animator animation;

void start()
{
animation = GetComponent();
}

void Update()
{

Movement();
Direction();
}

void Movement()
{
if (Input.GetKey(KeyCode.RightArrow)) //RIGHT
{
//Physically Move the Sprite Right
transform.Translate(Vector2.right * TrumpWalkSpeed * Time.deltaTime); //Transform current X Right by Speed and DeltaTime
//Call the animation walking state
animation.Play(“TrumpWalk”); //WONT WORK!!! NULEXCEPTION
//Make the clouds move in proportion
cloudlayer.transform.position += Vector3.right * TrumpWalkSpeed * Time.deltaTime;
TrumpDirection = 1;

}

if (Input.GetKey(KeyCode.LeftArrow))
{

//Physically Move the Sprite Left
transform.Translate(Vector2.left * (TrumpWalkSpeed + cloudoffset) * Time.deltaTime);
//Make the clouds move in proportion
cloudlayer.transform.position += Vector3.left * (TrumpWalkSpeed + cloudoffset) * Time.deltaTime;
//animated.transform.Rotate(0, 180, 0);
TrumpDirection = -1;

}
//TrumpDirection = 0;
}

I really do feel like a complete newb and getting so frustrated and come so far to be stuck on a simple pissant issue. Really pissing me off.

If there is anything else i can add, to help assist in TS, please let me know. Once i figure this out ill be able to finish all animations. www.trumpaustralia.net.au

whats your animation window look like? no transitions? I’d search youtube for “unity 2d walk” and see if you can’t find some simpler code for movement.

1 Like

Hey there,

Im still trying to get this working. After spending several more hours. I have changed my code but STILL GETTING NULLEXCEPTION errors??? I managed to setup the animationcontroller to transition on the basis of MovementType being = 1. If it is equal to 0 will go back to the idle state. As soon as i press the right arrow is when it stops and gives the error. Please help

void start()
{
anim = GetComponent();
Debug.Log(anim);

}

void Update()
{

Movement();
Direction();
}

void Movement()
{
if (Input.GetKey(KeyCode.RightArrow)) //RIGHT
{
//Physically Move the Sprite Right
transform.Translate(Vector2.right * TrumpWalkSpeed * Time.deltaTime); //Transform current X Right by Speed and DeltaTime
//Call the animation walking state
//Make the clouds move in proportion
cloudlayer.transform.position += Vector3.right * TrumpWalkSpeed * Time.deltaTime;
TrumpDirection = 1;
anim.SetInteger(“MovementType”,1);

}

OK i fixed it by making Anim Public and draging my gameobject (player) unto it. i was trying to drag the actuall animation controller

1 Like