- Home /
2D character running on the spot
Hey all im having a bit of a issue in unity where my 2D character is running on the spot and im not quite sure how to fix it. At first i was having trouble getting him to move using the A & D keys but after watching a new tutorial i finally got it to work but yea now it just runs on the spot :/ I'm still learning the ropes of unity and any help would be much appreciated.
here is the code i'm using which i learnt from a tutorial.
using UnityEngine;
using System.Collections;
public class CharacterMove : MonoBehaviour {
public float speed = 1.0f;
public string axisName = "Horizontal";
public Animator anim;
void Start () {
anim = gameObject.GetComponent<Animator>();
}
void Update () {
anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis(axisName)));
if(Input.GetAxis(axisName) < 0)
{
Vector3 newScale = transform.localScale;
newScale.x = 1.0f;
transform.localScale = newScale;
}
else if (Input.GetAxis(axisName) > 0)
{
Vector3 newScale = transform.localScale;
newScale.x = -1.0f;
transform.localScale = newScale;
}
transform.position += transform.right*Input.GetAxis(axisName)*speed*Time.deltaTime;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613163448im_/https://answers.unity.com/themes/thub/images/avi.jpg)