Created
April 2, 2018 19:45
-
-
Save ohiofi/8094f21408162c78a1dbee38808930c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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