Hello all,
I’m following a course from Udemy on building a 2D RPG game, and I’ve encountered a bug and would like some assistance on how to go about it.
The issue: my sprite is no longer able to move left and right, it can move up and down.
Some things I’ve tried:
1 - I’ve made a new sprite (a new gameObject) and I added a new script with the same code. This new sprite is able to move freely.
2 - I removed and re-added the Rigidbody2D component.
3 - I replaced the old script with my new script from my second sprite.
4 - Look in Debug mode of the Inspector and compared my two gameobjects.
Overall, I don’t think this is a coding issue. I think this is some issue with the game object itself.
In case you’re wondering, the code to move the sprite is quite simple:
public class NewController : MonoBehaviour
{
public Rigidbody2D rb2;
public float moveSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
rb2.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * moveSpeed;
}
}
Something to consider:
I do have animations on my sprite, and the animations behave as expected. It walks right when pressing right, and walks left when pressing left.
Using Debug.Log(), I can print out the velocity values and the sprite is definitely registering velocity in the x component (which is also seen by the animations) but it still won’t move! I did check the Constraints in the Rigidbody2D, there aren’t any.
Thanks in advance for any help,
-Brad