- Home /
Question by
Dr.FortuneC · Jun 11, 2015 at 10:41 AM ·
movement2d game
2d Character is not moving help..
public class Player : MonoBehaviour {
public float maxSpeed =3;
public float speed = 50f;
public float jumpPower = 150f;
public bool grounded;
private Rigidbody2D rb2d;
private Animator anim;
// Use this for initialization
void Start () {
rb2d = gameObject.GetComponent<Rigidbody2D>();
anim = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
anim.SetBool ("Grounded", grounded);
anim.SetFloat ("Speed", Mathf.Abs (Input.GetAxis ("Horizontal")));
// lets your characters face left if - and face right if +
if(Input.GetAxis("Horizontal") < -0.1f) {
transform.localScale = new Vector3(-1,1,1);
}
if(Input.GetAxis("Horizontal") > 0.1f) {
transform.localScale = new Vector3(1,1,1);
}
if(Input.GetButtonDown("Jump") && grounded){
rb2d.AddForce(Vector2.up * jumpPower);
}
}
//uses for physics movement
void fixedUpdate()
{
float h = Input.GetAxis("Horizontal");
//moving the player
rb2d.AddForce((Vector2.right * speed) * h);
// limiting the speed of the player
if(rb2d.velocity.x > maxSpeed)
{
rb2d.velocity = new Vector2(maxSpeed,rb2d.velocity.y);
}
if(rb2d.velocity.x < -maxSpeed)
{
rb2d.velocity = new Vector2(-maxSpeed,rb2d.velocity.y);
}
}
}
Help my character can jump , face left when i pressed A and right when i pressed D but it doesnt want to move..
Comment
Is your rigidbody2d kinematic? Is it sat ontop of a surface with a high friction? What is the mass of your rigidbody? It might be moving but by such a small amount you cannot see, perhaps multiply the output of Input.GetAxis by 100. You might be adding a force too small for it to make much difference.
C# is case sensitive so "fixedUpdate" should be "FixedUpdate"
Thank you guys for the reply.. but @maccabbe you are right... Thank you again :D