Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player_Move : MonoBehaviour {
  6.  
  7. public int playerSpeed = 10;
  8. public bool facingRight = true;
  9. public int playerJumpPower = 1250;
  10. public float moveX;
  11. public bool isGrounded;
  12.  
  13.  
  14. // Use this for initialization
  15. void Start() {
  16.  
  17. }
  18.  
  19. // Update is called once per frame
  20. void Update() {
  21. PlayerMove();
  22. }
  23. void PlayerMove() {
  24. //CONTROLS
  25. moveX = Input.GetAxis("Horizontal");
  26. if (Input.GetButtonDown("Jump") && isGrounded == true){
  27. Jump();
  28. }
  29. //ANIMATIONS
  30. //PLAYER DIRECTIONS
  31. if (moveX < 0.0f && facingRight == false) {
  32. FlipPlayer();
  33. } else if (moveX > 0.0f && facingRight == true) {
  34. FlipPlayer();
  35. }
  36. //PHYSICS
  37. gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);
  38. }
  39.  
  40. void Jump() {
  41. //JUMPING CODE
  42. GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpPower);
  43. isGrounded = false;
  44. }
  45.  
  46. void FlipPlayer() {
  47. facingRight = !facingRight;
  48. Vector2 localScale = gameObject.transform.localScale;
  49. localScale.x *= -1;
  50. transform.localScale = localScale;
  51. }
  52.  
  53. void onCollisionEnter2D (Collision2D col){
  54. Debug.Log ("Player has collided with " + col.collider.name);
  55. if (col.gameObject.tag == "ground") {
  56. isGrounded = true;
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement