- Home /
2D player keeps getting dragged to the left for some reason.
I'm trying to create a game that requires an xbox controller, yet after I programmed it in, my player character kept on moving to the left. Any ideas why? My current code is:
using UnityEngine; using System.Collections;
public class Movement : MonoBehaviour
{ public bool grounded; public float moveSpeed; public float jumpPower; Animator anim; Vector3 X; public int maxblasts1 = 0;
private Rigidbody2D rb;
void Start ()
{
SpriteRenderer sr = gameObject.GetComponent<SpriteRenderer> ();
rb = gameObject.GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
X = transform.localScale;
}
void Update ()
{
Rigidbody2D rb = GetComponent<Rigidbody2D> ();
SpriteRenderer sr = GetComponent<SpriteRenderer> ();
if (Input.GetAxis("HorizontalJoy") >= 0.1f) {
rb.AddForce (Vector2.right * moveSpeed);
sr.flipX = false;
}
if (Input.GetAxis("HorizontalJoy") <= 0.1f) {
rb.AddForce (Vector2.left * moveSpeed);
sr.flipX = true;
}
if (Input.GetAxis("Vertical") >= 0.1f)
rb.AddForce(Vector2.up * jumpPower);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to make the sprite move a certain amount of pixels when a key is pressed? 1 Answer
[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers
Player movement script for a stickman 1 Answer
How do i make a cube move (Continuosly without stopping) when i press a button once in unity 2D 2 Answers
Player movement boudaries in 2D 1 Answer