- Home /
2D character movement
I'm sure I messed up, I apologize in advance for the trivial question: i've some problem with the animation of my character, and the respective script, i've set a boolean for play (the animation) with the "D" letter but the problem is that actually, if i press it again my character does not move a second time. the question is: using a Boolean in my code is actually the right choice? If it's not, and this is the trivial part, i should insert rb2d.AddForce? When i used my animation didn't play and my character does not move. Thanks in advance mates.
@$$anonymous$$idori48 ,try this
public float moveSpeed;
public Rigidbody2D rb;
public Animator animator;
// Use this for initialization
void Start () {
animator = GetComponent<Animator> ();
rb = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
if(Input.GetAxisRaw("Horizontal")!=0){
rb.velocity = new Vector2 (moveSpeed,rb.velocity.y);
animator.Play ("RunAnimationName");
}
else if(Input.GetAxisRaw("Horizontal")==0){
rb.velocity = new Vector2 (0,rb.velocity.y);
animator.Play ("IdleAnimationName");
}
}
}
Answer by petarpejovic · Feb 07, 2018 at 02:11 PM
You can try with this :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
//[SerializeField]
public float maxSpeed = 10f; // Maximum speed player can have
//Facing of player. It can be Left or Right.
bool facingRight = true;
// Rigidbody 2D component you need to add to Player GameObject
private Rigidbody2D rb;
private Animator animator;
void Start ()
{
rb = GetComponent<Rigidbody2D>();
animator.GetComponent<Animator>();
}
// It is better to do this things like movement in Fixed Update
void FixedUpdate ()
{
///<summary> You dont need if statement to check GetKeyDown(). GetAxix("Horizontal") will check if Player pressed keys (Left or Right Arrow, A or D).
///You can also change this in Input Settings if you want
///</summary>
///
float move = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(move * maxSpeed, rb.velocity.y);
if(move > 0 && !facingRight) // Run Right
{
Flip();
animator.Play("RunAnimationName");
}
else if (move<0 && facingRight) // Run Left
{
Flip();
animator.Play("RunAnimationName");
}
else // Idle
{
animator.Play("IdleAnimationName");
}
}
/// <summary>
/// This function Flip player Sprite Left or Right
/// </summary>
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Thanks to you half of the problem is fixed (really, thanks for the help) but still, my character just move on the same spot, probably i should use something like transform.up(?).
No mate, i'm nuts, it works! :D (So damn happy and is just the beginning), thanks again!
it says "No $$anonymous$$onoBehaviour scripts in the file, or their names do not match the file name"
Your answer
![](https://koobas.hobune.stream/wayback/20220612152131im_/https://answers.unity.com/themes/thub/images/avi.jpg)