Guest User

Untitled

a guest
Oct 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour {
  6.  
  7. public float movePower = 1f;
  8. public float jumpPower = 1f;
  9.  
  10. Rigidbody2D rigid;
  11.  
  12. Vector3 movement;
  13. bool isJuming = false;
  14.  
  15.  
  16. // Use this for initialization
  17. void Start ()
  18. {
  19. rigid = gameObject.GetComponent<Rigidbody2D>();
  20. }
  21.  
  22. // Update is called once per frame
  23. void Update ()
  24. {
  25. if (Input.GetButtonDown("Jump"))
  26. {
  27. isJuming = true;
  28. }
  29.  
  30. }
  31.  
  32. void FixedUpdate()
  33. {
  34. Move();
  35. Jump();
  36. }
  37.  
  38. void Move()
  39. {
  40. Vector3 moveVelocity = Vector3.zero;
  41.  
  42. if (Input.GetAxisRaw("Horizontal") < 0)
  43. {
  44. moveVelocity = Vector3.left;
  45. }
  46. else if (Input.GetAxisRaw("Horizontal") > 0)
  47. {
  48. moveVelocity = Vector3.right;
  49. }
  50. transform.position += moveVelocity * movePower * Time.deltaTime;
  51. }
  52.  
  53. void Jump()
  54. {
  55. if (!isJuming)
  56. return;
  57.  
  58. rigid.velocity = Vector2.zero;
  59. Vector2 jumpVelocity = new Vector2(0, jumpPower);
  60. rigid.AddForce(jumpVelocity, ForceMode2D.Impulse);
  61.  
  62. isJuming = false;
  63. }
  64. }
Add Comment
Please, Sign In to add comment