- Home /
Question by
pacman9269 · Nov 24, 2014 at 09:35 AM ·
c#animatortopdown
Unity2d Top Down: Can't move character with Animator enabled
I am making a simple top down 2d game and have implemented a script from a tutorial in order to set up directional walking animations. The script works and plays my animations with key input, but my player will not move. I have also seen that if I disable the animator component on the player then the player will move without any animations being played.
The code for the script is:
using UnityEngine; using System.Collections;
public class playerController : MonoBehaviour {
private Animator anim;
void Start () {
anim = GetComponent<Animator>();
}
void Update () {
float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
ManageMovement(h, v);
}
void ManageMovement(float horizontal,float vertical) {
if (horizontal != 0f || vertical != 0f) {
anim.SetBool ("moving", true);
animateWalk (horizontal, vertical); }
else {
anim.SetBool ("moving", false); }
Vector3 movement = new Vector3 (horizontal,vertical, 0);
rigidbody2D.velocity = movement; } void animateWalk(float h,float v)
{ if(anim){ if ((v > 0)&&(v>h)) {
anim.SetInteger ("Direction", 1); }
if ((h > 0)&&(v<h)) {
anim.SetInteger ("Direction", 2); }
if ((v < 0)&&(v<h)) {
anim.SetInteger ("Direction", 3); }
if ((h < 0 )&&(v>h)) {
anim.SetInteger ("Direction", 4); } } } }
Any help with this would be appreciated as I want to get this project finished tomorrow.
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to change animation's speed in C#? 2 Answers
Dumb this down for me? 1 Answer