Skip to content

Instantly share code, notes, and snippets.

@ohiofi
Created April 2, 2018 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohiofi/8094f21408162c78a1dbee38808930c0 to your computer and use it in GitHub Desktop.
Save ohiofi/8094f21408162c78a1dbee38808930c0 to your computer and use it in GitHub Desktop.
[RequireComponent(typeof(SpriteRenderer),typeof(Rigidbody2D),typeof(Animator))]
public float speed = 14f;
public float accel = 6f;
private Vector2 input;
private SpriteRenderer sr;
private Rigidbody2D rb;
private Animator animator;
sr = GetComponent<SpriteRenderer> ();
animator = GetComponent<Animator> ();
rb = GetComponent<Rigidbody2D> ();
input.x = Input.GetAxis ("Horizontal");
input.y = Input.GetAxis ("Jump");
if (input.x > 0f) {
sr.flipX = false;
} else if (input.x <0f){
sr.flipX = true;
}
var acceleration = accel;
var xVelocity = 0f;
if (input.x == 0) {
xVelocity = 0f;
} else {
xVelocity = rb.velocity.x;
}
rb.AddForce (new Vector2 (((input.x * speed) - rb.velocity.x) * acceleration, 0));
rb.velocity = new Vector2 (xVelocity, rb.velocity.y);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment