- Home /
Problems with animation and movement
Hello, I'm currently trying to make my player character move around the map, but for some reason, it will not move left and right (it will play the animation trigger however) and it can only move up and down in a restricted space. Currently my code for movement looks like this: public class PlayerMovement : MonoBehaviour {
[SerializeField]
private float speed;
[SerializeField]
private Animator animator;
private void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector2 currentVelocity = gameObject.GetComponent<Rigidbody2D>().velocity;
float newVelocityX = 0f;
if (moveHorizontal < 0 && currentVelocity.x <= 0)
{
newVelocityX = -speed;
animator.SetInteger("DirectionX", -1);
}
else if (moveHorizontal > 0 && currentVelocity.x >= 0)
{
newVelocityX = speed;
animator.SetInteger("DirectionX", 1);
}
else
{
animator.SetInteger("DirectionX", 0);
}
float newVelocityY = 0f;
if (moveVertical < 0 && currentVelocity.y <= 0)
{
newVelocityY = -speed;
animator.SetInteger("DirectionY", -1);
}
else if (moveVertical > 0 && currentVelocity.y >= 0)
{
newVelocityY = speed;
animator.SetInteger("DirectionY", 1);
}
else
{
animator.SetInteger("DirectionY", 0);
}
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(newVelocityX, newVelocityY);
}
I'm not entirely sure where I went wrong, and why my character isn't moving left and right. I'm wondering if it has something to do with my map, since all objects are on one layer, meaning the ground and collidable objects such as trees have sort of overlapped, but I don't know how to separate them into different layers.
If anyone knows how to help, I'd really appreciate it.
Does your player's rigidbody has freezed positions of some axis?
Answer by tristantcate · Nov 23, 2018 at 01:57 PM
Hi GameCreater562,
In terms of functionality, there's nothing wrong with your script. You should be able to move left and right. It probably has to do with colliders like you said.
Luckily you can easily modify what collides with what in Unity. I refer to layer system. You can give every collider a layer just as you would a tag. Then if you go to Edit > Project Settings > Physics2D (considering you're working in a 2D project).
Then you'll get a collision matrix. Shown below is default. You will see your own custom added layers in here as well.
In here you can simply uncheck the boxes for the objects you wouldn't want to collide with one another (for instance if there are colliders on things you don't want to collide with the player).
Let me know if this helped, and good luck on your project :)
Thanks for your help, I was just wondering how I can sort my tiles into layers?
Your answer
Follow this Question
Related Questions
Animator behaviour on GameObject 1 Answer
Animation Lag when Changing Movement Direction 2D (4.3) 2 Answers
Mixamo strafing animation moving slightly on the Z-Axis 0 Answers
Animator & movements 1 Answer
Twin-Stick Movement 1 Answer