Question by
loafy123 · Jul 30, 2021 at 01:25 PM ·
animationanimatorunity 2dcharacter movement
Character won't move
I am doing a character with animation that can move up, down, left and right. Before I insert animation the character can move with arrows. But after adding animation, pressing arrows will trigger the correct animations (I only use two animations, idle and walk), but the character won't move. How do I fix this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player_movement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D myBody;
Vector2 movement;
public Animator anim;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//Input.GetAxisRaw("Horizontal");
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
}
void FixedUpdate()
{
CharaMovement();
myBody.MovePosition(myBody.position + movement * moveSpeed * Time.fixedDeltaTime);
}
void CharaMovement()
{
float h =Input.GetAxisRaw("Horizontal");
if(h>0)
{
myBody.velocity = new Vector2(moveSpeed * h, myBody.velocity.y);
Vector3 scale = transform.localScale;
scale.x = 0.4f;
transform.localScale = scale;
anim.SetBool("walk", true);
}
else if(h<0)
{
myBody.velocity = new Vector2(moveSpeed * h, myBody.velocity.y);
Vector3 scale = transform.localScale;
scale.x = -0.4f;
transform.localScale = scale;
anim.SetBool("walk", true);
}
else
{
myBody.velocity = new Vector2(0, myBody.velocity.y);
anim.SetBool("walk", false);
}
}
}
Comment
Make sure the root animation option on the animations is unchecked......this can cause such problems
Your answer
Follow this Question
Related Questions
Animator Position VS Script Vector 3 0 Answers
Apk on android does not start. Reason: Animator | Не запускается Apk на андройд. Причина: Animator 0 Answers
One script to call animation on different objects 0 Answers
I have an animation system going but.... 0 Answers
My animations started acting weirdly 0 Answers