- Home /
Left/Right movement 2D game,Why is this code to make my character move left/right not working?
I am trying to make my character move left/right in my 2D platformer but it won't allow me to, here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float Movespeed;
private Rigidbody2D myRigidBody;
// Start is called before the first frame update
private void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
private void Update() {
if (Input.GetAxisRaw("Horizontal") >0f)
{
myRigidBody.velocity = new Vector3(Movespeed, myRigidBody.velocity.y, 0f);
} else if (Input.GetAxisRaw("Horizontal") <0f)
{
myRigidBody.velocity = new Vector3(-Movespeed, myRigidBody.velocity.y, 0f);
} else
{
myRigidBody.velocity = new Vector3(0f, myRigidBody.velocity.y, 0f);
}
}
} ,This is my code to try and make my character move left/right: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float Movespeed;
public Rigidbody2D myRigidBody;
// Start is called before the first frame update
private void Start()
{
myRigidBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
private void Update() {
if (Input.GetAxisRaw("Horizontal") > 0f)
{
myRigidBody.velocity = new Vector3(Movespeed, myRigidBody.velocity.y, 0f);
} else if (Input.GetAxisRaw("Horizontal") < 0f)
{
myRigidBody.velocity = new Vector3(-Movespeed, myRigidBody.velocity.y, 0f);
} else
{
myRigidBody.velocity = new Vector3(0f, myRigidBody.velocity.y, 0f);
}
}
}
Are you sure $$anonymous$$ovespeed
has a value greater than 0
in the inspector?
Your answer
Follow this Question
Related Questions
,Spawning snow or changing tilesets to snow as the player walks past them 0 Answers
so I'm trying to make a 2D platform but the first animation i make is the only one that registers 0 Answers
Having some stuck issues on the 2D infinite runner 0 Answers
Switch Players during play 2 Answers
Player getting stuck between grounds 0 Answers