- Home /
setting up animations
have animations i have done it before but i cant remeber how i was wondering if something could look at my code and see how i have failed when i try to move to activate the animation it doesnt move and when i comment it out and put it back to normal it moves
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public float Speed = 5.0f;
Animator anim;
public bool Ismoving;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
}
// Update is called once per frame
public void Update () {
var x = Input.GetAxis ("Horizontal") * Speed * Time.deltaTime;
var z = Input.GetAxis ("Vertical") * Speed * Time.deltaTime;
transform.Translate (x, 0, z);
Ismoving = true;
anim.SetFloat ("Speed", Speed);
if(Input.GetAxis("Horizontal") && Input.GetAxis("Vertical") == null){
Ismoving = false;
anim.SetFloat ("Speed", 0);
}
}
}
How are we supposed to know how you failed? What is happening? What should happen?
sorry i forgot lol i was making this question mid lecture basically when i try to apply animations it cancls the movement
Answer by sh_code · Nov 09, 2016 at 12:29 PM
afaik, Input.GetAxis never returns null (unless maybe when said axis doesn't exist at all). When the axes get no input, they should be zero. so the condition is wrong and to correct it, you need to change it to
if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)
secondly, other than that, code doesn't seem to have anything outright wrong with it, so I'd check the Mecanim state machine, animation transition settings and conditions, as these errors are usually either there, or arise from incorrect interaction of code and the state machine settings.
Nothing more can be inferred from the sparse info you've provided.
thirdly, @rooster2, it would be nice of you to specify what was the error, because I doubt my first point was the only, or the main part of the issue. you explaining what the issue really was after fixing it will not only provide the people who tried to answer a closure, and the feeling that motivates them to help, but will also provide a possible ready-made answer for others after you facing the same/similar issue ;)
it was our frst point and i cant remeber what it was but something about not being able to do something with floats and inputs i was bored in a lecture so i decided id do some on my game but got stuck and you help origato
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Play Animation with int (ammo count) 0 Answers